Create more sophisticated decision-making with nested conditions, logical operators, and switch statements for cleaner code.
🎯 Key Skills You'll Learn:
Nested if statements
Complex logical conditions
Switch statements
Ternary operator
🚀 Session Project:
Smart Calculator with Error Checking
Learning Objectives
Learn about nested if statements
Understand complex logical conditions
Practice combining multiple conditions
Create more sophisticated decision-making programs
Warm-up (5 minutes)
Modify your Rock, Paper, Scissors game from last session. What would you need to change to make the computer pick a random choice instead of always the same one?
New Concept Introduction (20 minutes)
Nested If Statements
Sometimes you need to check one condition inside another:
#include<iostream>usingnamespace std;intmain(){int age;bool hasLicense; cout <<"Enter your age: "; cin >> age; cout <<"Do you have a driver's license? (1 for yes, 0 for no): "; cin >> hasLicense;if(age >=16){if(hasLicense){ cout <<"You can drive!"<< endl;}else{ cout <<"You're old enough, but need a license."<< endl;}}else{ cout <<"You're too young to drive."<< endl;}return0;}
Complex Logical Conditions
You can combine multiple conditions in one if statement:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Using AND (&&)
if(age >=16&& hasLicense){ cout <<"You can drive!"<< endl;}// Using OR (||)
if(grade =='A'|| grade =='B'){ cout <<"Great job!"<< endl;}// Combining AND and OR (use parentheses for clarity)
if((age >=16&& hasLicense)||(age >=18)){ cout <<"You have driving privileges!"<< endl;}
Switch Statements
For checking many specific values, switch can be cleaner than multiple if-else:
// Long way
string message;if(age >=18){ message ="Adult";}else{ message ="Minor";}// Short way (ternary operator)
string message =(age >=18)?"Adult":"Minor";
#include<iostream>usingnamespace std;intmain(){int age;char rating;bool withParent; cout <<"Enter your age: "; cin >> age; cout <<"Enter movie rating (G, P, M, R): "; cin >> rating;switch(rating){case'G': cout <<"Anyone can watch this movie!"<< endl;break;case'P':if(age >=13){ cout <<"You can watch this movie!"<< endl;}else{ cout <<"Do you have a parent with you? (1 for yes, 0 for no): "; cin >> withParent;if(withParent){ cout <<"You can watch with parental guidance!"<< endl;}else{ cout <<"Sorry, you need a parent to watch this movie."<< endl;}}break;case'M':if(age >=15){ cout <<"You can watch this movie!"<< endl;}else{ cout <<"Sorry, you must be 15 or older."<< endl;}break;case'R':if(age >=18){ cout <<"You can watch this movie!"<< endl;}else{ cout <<"Sorry, you must be 18 or older."<< endl;}break;default: cout <<"Invalid rating entered."<< endl;}return0;}
Challenge/Quiz (10 minutes)
Challenge: Smart Calculator
Create a calculator that:
Asks for two numbers
Asks for an operation (+, -, *, /)
Performs the calculation
Has these smart features:
Prevents division by zero
For negative results, asks if user wants the absolute value
For division, warns if the result will have a remainder
Asks if user wants to do another calculation
Use nested if statements and logical operators appropriately.
Validation Prompt
🤖 AI Validation Prompt
Copy this prompt and your code to ChatGPT or Claude for instant feedback:
```
I'm learning C++ and just completed Session 7. Here's my solution to the Smart Calculator challenge:
[PASTE YOUR CODE HERE]
The challenge was: Create a smart calculator with error checking, user options, and helpful warnings.
Please check if my solution:
1. Uses nested if statements appropriately
2. Prevents division by zero
3. Uses switch statement or if-else for operations
4. Implements the smart features correctly
5. Has clear user interaction and error messages
If there are issues, please explain what's wrong and give me a hint to fix it (don't give me the complete solution).
```
What You Learned
✅ Nested if statements for complex logic
✅ Combining conditions with && and ||
✅ Switch statements for multiple specific values
✅ Ternary operator for simple conditions
✅ Error checking and input validation
Next Session Preview
In Session 8, you’ll learn about loops - how to make your programs repeat actions automatically!