Module 3: Decision Making in C++
Decision making allows a program to execute different blocks of code based on specific conditions. It helps create dynamic and interactive applications by evaluating whether a condition is true or false.
For example:
- Checking if a user is eligible to vote.
- Determining whether a student has passed or failed.
- Displaying different menu options based on user input.
1. if Statement
The if statement executes a block of code only if the specified condition is true.
Syntax
1if (condition) 2{ 3 // Code to execute if condition is true 4}
Example
1#include <iostream> 2 3using namespace std; 4 5int main() 6{ 7 int age = 20; 8 9 if (age >= 18) 10 { 11 cout << "Eligible to Vote"; 12 } 13 14 return 0; 15}
Output
1Eligible to Vote
2. if...else Statement
The if...else statement executes one block if the condition is true and another block if it is false.
Syntax
1if (condition) 2{ 3 // True block 4} 5else 6{ 7 // False block 8}
Example
1#include <iostream> 2 3using namespace std; 4 5int main() 6{ 7 int marks = 45; 8 9 if (marks >= 50) 10 { 11 cout << "Pass"; 12 } 13 else 14 { 15 cout << "Fail"; 16 } 17 18 return 0; 19}
Output
1Fail
3. Nested if
A nested if is an if statement inside another if statement. It is useful when multiple conditions need to be checked.
Syntax
1if (condition1) 2{ 3 if (condition2) 4 { 5 // Code 6 } 7}
Example
1#include <iostream> 2 3using namespace std; 4 5int main() 6{ 7 int age = 22; 8 bool hasLicense = true; 9 10 if (age >= 18) 11 { 12 if (hasLicense) 13 { 14 cout << "You can drive."; 15 } 16 else 17 { 18 cout << "License required."; 19 } 20 } 21 else 22 { 23 cout << "You are underage."; 24 } 25 26 return 0; 27}
Output
1You can drive.
4. switch Statement
The switch statement selects one block of code from multiple options based on the value of an expression.
Syntax
1switch (expression) 2{ 3 case value1: 4 // Code 5 break; 6 7 case value2: 8 // Code 9 break; 10 11 default: 12 // Code 13}
Example
1#include <iostream> 2 3using namespace std; 4 5int main() 6{ 7 int day = 3; 8 9 switch (day) 10 { 11 case 1: 12 cout << "Monday"; 13 break; 14 15 case 2: 16 cout << "Tuesday"; 17 break; 18 19 case 3: 20 cout << "Wednesday"; 21 break; 22 23 default: 24 cout << "Invalid Day"; 25 } 26 27 return 0; 28}
Output
1Wednesday
Note: The
breakstatement prevents execution from continuing into the next case.
5. Conditional (Ternary) Operator
The conditional operator (?:) is a shorthand version of the if...else statement.
Syntax
1condition ? expression1 : expression2;
Example
1#include <iostream> 2 3using namespace std; 4 5int main() 6{ 7 int age = 18; 8 9 string result = (age >= 18) ? "Adult" : "Minor"; 10 11 cout << result; 12 13 return 0; 14}
Output
1Adult
Comparison of Decision Statements
| Statement | Purpose |
|---|---|
if | Executes code when a condition is true |
if...else | Chooses between two code blocks |
Nested if | Checks multiple conditions |
switch | Selects one option from many |
| Ternary Operator | Short form of if...else |
Best Practices
- Use meaningful conditions.
- Keep nested
ifstatements to a minimum for better readability. - Always include
breakinswitchcases unless fall-through is intentional. - Use the ternary operator only for simple conditions.
- Indent code properly to improve readability.
Common Mistakes
Using = Instead of ==
❌ Incorrect
1if (age = 18)
✅ Correct
1if (age == 18)
Forgetting break in switch
1switch (choice) 2{ 3 case 1: 4 cout << "Option 1"; 5 break; 6 7 case 2: 8 cout << "Option 2"; 9 break; 10}
Without break, execution continues into the next case.
Practice Exercises
- Check whether a person is eligible to vote.
- Find the larger of two numbers using
if...else. - Check if a year is a leap year.
- Create a simple calculator using
switch. - Use the ternary operator to determine whether a number is even or odd.
Interview Questions
- What is decision making in C++?
- What is the difference between
ifandif...else? - When should you use a nested
if? - What is the purpose of the
switchstatement? - Why is the
breakstatement used in aswitch? - What is the conditional (ternary) operator?
- When is the ternary operator preferred over
if...else?
Summary
In this module, you learned:
- How the
ifstatement executes code based on a condition. - How
if...elseprovides two possible execution paths. - How nested
ifstatements evaluate multiple conditions. - How the
switchstatement selects from multiple choices. - How the conditional (ternary) operator offers a concise alternative to
if...else.
In the next module, you'll learn Loops in C++, including for, while, do...while, range-based for loops, nested loops, and pattern printing.