Conditional Statements
Conditional statements allow your program to make decisions and execute different code paths based on certain conditions. They are the foundation of program logic and enable your code to respond to different situations.
If Statement
The if statement executes code only when a condition is true:
#include
using namespace std;
int main() {
int age;
cout << "Enter your age: ";
cin >> age;
if (age >= 18) {
cout << "You are an adult!" << endl;
}
if (age < 13) {
cout << "You are a child!" << endl;
}
return 0;
}
If Statement Syntax:
- Condition must be in parentheses
- Code block is enclosed in curly braces
- Condition evaluates to true or false
- Braces are optional for single statements
If-Else Statement
The if-else statement provides an alternative path when the condition is false:
int score;
cout << "Enter your test score: ";
cin >> score;
if (score >= 60) {
cout << "You passed!" << endl;
} else {
cout << "You failed. Study harder!" << endl;
}
// Another example with multiple conditions
if (score >= 90) {
cout << "Grade: A" << endl;
} else if (score >= 80) {
cout << "Grade: B" << endl;
} else if (score >= 70) {
cout << "Grade: C" << endl;
} else if (score >= 60) {
cout << "Grade: D" << endl;
} else {
cout << "Grade: F" << endl;
}
Nested If Statements
You can place if statements inside other if statements for complex decision making:
int temperature, humidity;
cout << "Enter temperature (F) and humidity (%): ";
cin >> temperature >> humidity;
if (temperature > 70) {
if (humidity > 60) {
cout << "It's hot and humid!" << endl;
} else {
cout << "It's hot but dry." << endl;
}
} else {
if (humidity > 60) {
cout << "It's cool and humid." << endl;
} else {
cout << "It's cool and dry." << endl;
}
}
Best Practice:
Avoid excessive nesting as it makes code hard to read. Consider using logical operators or separate functions for complex conditions.
Switch Statement
The switch statement is useful when you have multiple possible values for a single variable:
char grade;
cout << "Enter your letter grade: ";
cin >> grade;
switch (grade) {
case 'A':
case 'a':
cout << "Excellent! (90-100)" << endl;
break;
case 'B':
case 'b':
cout << "Good! (80-89)" << endl;
break;
case 'C':
case 'c':
cout << "Average (70-79)" << endl;
break;
case 'D':
case 'd':
cout << "Below Average (60-69)" << endl;
break;
case 'F':
case 'f':
cout << "Failing (below 60)" << endl;
break;
default:
cout << "Invalid grade entered!" << endl;
break;
}
Switch Statement Rules:
- Only works with integers, characters, and enums
- Each case must end with
break;
to prevent fall-through - Use
default:
for cases not explicitly handled - Multiple cases can share the same code block
Ternary Operator
The ternary operator provides a shorthand way to write simple if-else statements:
int a = 10, b = 20;
// Traditional if-else
int max1;
if (a > b) {
max1 = a;
} else {
max1 = b;
}
// Ternary operator (shorter)
int max2 = (a > b) ? a : b;
cout << "Maximum: " << max2 << endl;
// More examples
string status = (age >= 18) ? "Adult" : "Minor";
cout << "Status: " << status << endl;
// Can be nested (but avoid for readability)
string result = (score >= 90) ? "A" :
(score >= 80) ? "B" :
(score >= 70) ? "C" : "F";
Common Patterns and Best Practices
Range Checking
// Check if value is in range
if (x >= 1 && x <= 100) {
cout << "Valid range" << endl;
}
// Check multiple ranges
if (x < 0 || x > 100) {
cout << "Out of range" << endl;
}
String Comparison
string input;
cin >> input;
if (input == "yes" || input == "y") {
cout << "Confirmed!" << endl;
} else if (input == "no" || input == "n") {
cout << "Cancelled." << endl;
}
Performance Tips:
- Put the most likely conditions first in if-else chains
- Use switch for many equality comparisons on the same variable
- Combine conditions with logical operators when appropriate
- Consider early returns to reduce nesting