01 / 21

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.

C++

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.

Start Condition true or false? true Action A false Action B Continue

The if Statement

The simplest conditional: if the condition is true, run the code inside the braces. If it's false, skip it entirely.

C++
if (condition) {
    // runs only when condition is true
}
Example
int temperature = 105;

if (temperature > 100) {
    cout << "It's boiling!" << endl;
}
Output
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.

C++
int age = 15;

if (age >= 18) {
    cout << "You can vote." << endl;
} else {
    cout << "Too young to vote." << endl;
}
Output
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.

\(==\)
Equal to
\(\neq\)
Not equal to
\(<\)  \(>\)
Less than / Greater than
\(\le\)  \(\ge\)
Less or equal / Greater or equal

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.

Syntax
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

\( \text{score} \ge 90\text{?} \)
true Print "A" false
\( \text{score} \ge 80\text{?} \)
true Print "B" false
\( \text{score} \ge 70\text{?} \)
true Print "C" false Print "F"

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

Separate if statements
Each condition is checked independently. Multiple blocks can run.
else if chain
Conditions are checked in order. Only the first true block runs.
Separate if's (both can run!)
int x = 10;
if (x > 5)  cout << "A";  // runs
if (x > 8)  cout << "B";  // also runs
// Output: AB
Else-if chain (only first match runs)
int x = 10;
if (x > 5)       cout << "A";  // runs
else if (x > 8)  cout << "B";  // skipped!
// Output: A

Step-by-Step Approach

1
Identify the decision. What question does your program need to answer?
2
Count the outcomes. Two outcomes: use if/else. Three or more: use an else-if chain.
3
Order conditions. Put the most specific or restrictive condition first (e.g., check \( \text{score} \ge 90 \) before \( \text{score} \ge 80 \)).
4
Add a final else. Use a catch-all else at the end to handle unexpected input.
5
Test with edge cases. Try boundary values like exactly 90, 80, 70, and 0.

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.

C++
#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;
}
Output
7 is odd.

Example 2: Grade Calculator

C++
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;
}
Output
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.

C++
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;
}
Output
Entry allowed.

Logical Operators in Conditions

&& (AND)
Both sides must be true for the whole condition to be true.
|| (OR)
At least one side must be true for the whole condition to be true.
! (NOT)
Flips true to false and false to true.
Example
int temp = 72;

if (temp >= 60 && temp <= 80) {
    cout << "Nice weather!";
} else if (temp > 80 || temp < 30) {
    cout << "Extreme weather!";
}

Common Mistakes

✕ Using = instead of ==
if (x = 5) assigns 5 to x (always true). Use == for comparison.
✕ Wrong condition order
Checking \( \text{score} \ge 70 \) before \( \text{score} \ge 90 \) means an A student gets a C.
✕ Missing braces
Without { }, only the first line after if is conditional. The rest always runs.
✕ Forgetting else
Without a final else, unexpected input silently produces no output.

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;
if (
\(x > 20\)) cout << "A";
else if (
\(x > 10\)) 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;
if (
\(n > 5\)) cout << "X";
if (
\(n > 8\)) 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";