If/Else &
Else-If Chains
Master conditional branching in C++ to make your programs respond to different situations with if, else, and else if statements.
What Is a Conditional?
A conditional statement lets your program choose which code to run based on whether a condition is true or false. Without conditionals, every program would run the exact same instructions every time.
The if Statement
The simplest conditional: if the condition is true, run the code inside the braces. If it's false, skip it entirely.
if (condition) { // runs only when condition is true }
int temperature = 105; if (temperature > 100) { cout << "It's boiling!" << endl; }
It's boiling!
Adding else: The Other Path
The else block runs when the if condition is false. Together they guarantee exactly one block always executes.
int age = 15; if (age >= 18) { cout << "You can vote." << endl; } else { cout << "Too young to vote." << endl; }
Too young to vote.
Boolean Conditions
Every condition inside an if must evaluate to true or false. C++ gives you six comparison operators to build these conditions.
The else if Chain
When you have more than two possibilities, chain multiple conditions together. The program checks each in order and runs the first one that is true.
if (condition1) { // runs if condition1 is true } else if (condition2) { // runs if condition1 is false AND condition2 is true } else if (condition3) { // runs if both above are false AND condition3 is true } else { // runs if ALL conditions above are false }
How an Else-If Chain Flows
if vs if/else vs else-if chain
| Feature | if | if/else | else-if chain |
|---|---|---|---|
| Branches | 1 | 2 | 2+ |
| Guarantees execution? | No | Yes | Yes (with else) |
| Multiple conditions | No | No | Yes |
| Blocks that run | 0 or 1 | Exactly 1 | Exactly 1 |
Separate if's vs else if
int x = 10; if (x > 5) cout << "A"; // runs if (x > 8) cout << "B"; // also runs // Output: AB
int x = 10; if (x > 5) cout << "A"; // runs else if (x > 8) cout << "B"; // skipped! // Output: A
Step-by-Step Approach
Example 1: Even or Odd
Use the modulo operator % to check if a number is divisible by 2. If the remainder is 0, it's even; otherwise, it's odd.
#include <iostream> using namespace std; int main() { int num = 7; if (num % 2 == 0) { cout << num << " is even." << endl; } else { cout << num << " is odd." << endl; } return 0; }
7 is odd.
Example 2: Grade Calculator
int score = 85; if (score >= 90) { cout << "Grade: A" << endl; } else if (score >= 80) { cout << "Grade: B" << endl; } else if (score >= 70) { cout << "Grade: C" << endl; } else { cout << "Grade: F" << endl; }
Grade: B
Example 3: Nested Conditions
You can place an if statement inside another if to check multiple layers of logic. This is called nesting.
int age = 20; bool hasID = true; if (age >= 18) { if (hasID) { cout << "Entry allowed." << endl; } else { cout << "Need ID." << endl; } } else { cout << "Too young." << endl; }
Entry allowed.
Logical Operators in Conditions
int temp = 72; if (temp >= 60 && temp <= 80) { cout << "Nice weather!"; } else if (temp > 80 || temp < 30) { cout << "Extreme weather!"; }
Common Mistakes
if (x = 5) assigns 5 to x (always true). Use == for comparison.{ }, only the first line after if is conditional. The rest always runs.Quick Reference Summary
| Pattern | Syntax | When to Use |
|---|---|---|
| if | if (cond) { ... } |
One optional action |
| if/else | if (cond) { } else { } |
Exactly two outcomes |
| else-if chain | if / else if / else |
Three+ outcomes |
| Nested if | if { if { } } |
Multi-layer logic |
| && / || | if (a && b) |
Combine conditions |
Question 1 of 5
What does the following code print?int x = 15;\(x > 20\)
if () cout << "A";\(x > 10\)
else if () cout << "B";
else cout << "C";
Question 2 of 5
Which operator is used for comparison (not assignment) in C++?
Question 3 of 5
What is the output?int n = 10;\(n > 5\)
if () cout << "X";\(n > 8\)
if () cout << "Y";
Question 4 of 5
In a grade calculator that uses an else-if chain, why should you check \( \text{score} \ge 90 \) before \( \text{score} \ge 80 \)?
Question 5 of 5
What is wrong with this code?if (\(x = 10\))
cout << "Ten";