Bird
Raised Fist0
C++programming~20 mins

Else–if ladder in C++ - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Else-if Ladder Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of an else-if ladder with integer input
What is the output of the following C++ code when num = 15?
C++
#include <iostream>
using namespace std;

int main() {
    int num = 15;
    if (num < 10) {
        cout << "Less than 10";
    } else if (num < 20) {
        cout << "Between 10 and 19";
    } else if (num < 30) {
        cout << "Between 20 and 29";
    } else {
        cout << "30 or more";
    }
    return 0;
}
ABetween 10 and 19
BLess than 10
CBetween 20 and 29
D30 or more
Attempts:
2 left
💡 Hint
Check which condition is true first in the else-if ladder.
❓ Predict Output
intermediate
2:00remaining
Output when multiple else-if conditions are false
What will the program print when score = 85 in this else-if ladder?
C++
#include <iostream>
using namespace std;

int main() {
    int score = 85;
    if (score >= 90) {
        cout << "Grade A";
    } else if (score >= 80) {
        cout << "Grade B";
    } else if (score >= 70) {
        cout << "Grade C";
    } else {
        cout << "Fail";
    }
    return 0;
}
AGrade A
BGrade B
CGrade C
DFail
Attempts:
2 left
💡 Hint
Check the conditions from top to bottom carefully.
🔧 Debug
advanced
2:00remaining
Identify the error in this else-if ladder
What error will this code produce when compiled?
C++
#include <iostream>
using namespace std;

int main() {
    int x = 5;
    if (x > 0)
        cout << "Positive";
    else if x == 0 {
        cout << "Zero";
    } else {
        cout << "Negative";
    }
    return 0;
}
ALogical error, prints wrong output
BRuntime error due to uninitialized variable
CNo error, prints "Positive"
DSyntax error due to missing parentheses in else if condition
Attempts:
2 left
💡 Hint
Check the syntax of the else-if statement carefully.
🧠 Conceptual
advanced
2:00remaining
Understanding else-if ladder flow
Consider this else-if ladder. Which statement is true about how it executes?
C++
if (a > b) {
    // block 1
} else if (a == b) {
    // block 2
} else {
    // block 3
}
AAll blocks can execute if conditions are true
BBlocks 1 and 3 can execute together
COnly one block executes depending on the first true condition
DBlocks 2 and 3 always execute together
Attempts:
2 left
💡 Hint
Think about how else-if ladder chooses which block to run.
❓ Predict Output
expert
2:00remaining
Output of nested else-if ladder with multiple variables
What is the output of this C++ program?
C++
#include <iostream>
using namespace std;

int main() {
    int x = 10, y = 20;
    if (x > y) {
        cout << "X is greater";
    } else if (x == y) {
        cout << "X equals Y";
    } else if (y - x == 10) {
        cout << "Difference is 10";
    } else {
        cout << "Other case";
    }
    return 0;
}
ADifference is 10
BX equals Y
CX is greater
DOther case
Attempts:
2 left
💡 Hint
Check each condition carefully in order.

Practice

(1/5)
1.

What does an else-if ladder do in C++?

easy
A. Skips all conditions and runs the else block always
B. Runs all conditions regardless of their truth
C. Only runs the last condition in the ladder
D. Checks multiple conditions one by one and runs the first true block

Solution

  1. Step 1: Understand the purpose of else-if ladder

    An else-if ladder is used to check multiple conditions in order, one after another.
  2. Step 2: Identify how conditions are evaluated

    It runs the code block for the first condition that is true and skips the rest.
  3. Final Answer:

    Checks multiple conditions one by one and runs the first true block -> Option D
  4. Quick Check:

    Else-if ladder = first true condition runs [OK]
Hint: Remember: else-if stops at first true condition [OK]
Common Mistakes:
  • Thinking all conditions run
  • Believing else always runs
  • Confusing else-if with separate ifs
2.

Which of the following is the correct syntax for an else-if ladder in C++?

int x = 10;
if (x < 5) {
  // code A
} _____ (x < 15) {
  // code B
} else {
  // code C
}
easy
A. else if
B. elseif
C. else_if
D. else if()

Solution

  1. Step 1: Recall correct else-if syntax in C++

    The correct keyword is else if with a space, no underscores or parentheses after else.
  2. Step 2: Check each option

    Options A, B, and C are invalid syntax in C++.
  3. Final Answer:

    else if -> Option A
  4. Quick Check:

    Correct syntax = else if [OK]
Hint: Use 'else if' with space, no underscores or parentheses [OK]
Common Mistakes:
  • Writing 'elseif' as one word
  • Using underscores like 'else_if'
  • Adding parentheses after else
3.

What will be the output of this C++ code?

int num = 7;
if (num > 10) {
  std::cout << "Greater than 10";
} else if (num > 5) {
  std::cout << "Greater than 5";
} else {
  std::cout << "5 or less";
}
medium
A. Greater than 5
B. 5 or less
C. Greater than 10
D. No output

Solution

  1. Step 1: Evaluate first condition (num > 10)

    7 is not greater than 10, so this block is skipped.
  2. Step 2: Evaluate second condition (num > 5)

    7 is greater than 5, so this block runs and prints "Greater than 5".
  3. Final Answer:

    Greater than 5 -> Option A
  4. Quick Check:

    7 > 5 = true, prints "Greater than 5" [OK]
Hint: Check conditions top to bottom, first true runs [OK]
Common Mistakes:
  • Choosing first condition output incorrectly
  • Ignoring else-if order
  • Assuming else runs always
4.

Find the error in this else-if ladder code snippet:

int score = 85;
if (score >= 90) {
  std::cout << "Grade A";
} else if score >= 80 {
  std::cout << "Grade B";
} else {
  std::cout << "Grade C";
}
medium
A. Missing semicolon after std::cout
B. Missing parentheses around condition in else-if
C. Incorrect use of else instead of else-if
D. Using >= instead of >

Solution

  1. Step 1: Check syntax of else-if condition

    In C++, conditions must be inside parentheses. The else-if line lacks parentheses.
  2. Step 2: Verify other parts

    Semicolons and operators are correct; only parentheses are missing.
  3. Final Answer:

    Missing parentheses around condition in else-if -> Option B
  4. Quick Check:

    Conditions need parentheses [OK]
Hint: Always put conditions inside parentheses [OK]
Common Mistakes:
  • Forgetting parentheses in if/else-if
  • Missing semicolons after statements
  • Confusing else and else-if syntax
5.

Write an else-if ladder to print the day of the week given an integer day (1 to 7). What will be printed if day = 5?

int day = 5;
if (day == 1) {
  std::cout << "Monday";
} else if (day == 2) {
  std::cout << "Tuesday";
} else if (day == 3) {
  std::cout << "Wednesday";
} else if (day == 4) {
  std::cout << "Thursday";
} else if (day == 5) {
  std::cout << "Friday";
} else if (day == 6) {
  std::cout << "Saturday";
} else if (day == 7) {
  std::cout << "Sunday";
} else {
  std::cout << "Invalid day";
}
hard
A. Thursday
B. Saturday
C. Friday
D. Invalid day

Solution

  1. Step 1: Match the value of day with conditions

    day is 5, so the else-if condition day == 5 is true.
  2. Step 2: Identify the output for day == 5

    The code prints "Friday" for day 5.
  3. Final Answer:

    Friday -> Option C
  4. Quick Check:

    day 5 = Friday [OK]
Hint: Match day number to correct else-if condition [OK]
Common Mistakes:
  • Mixing up day numbers and names
  • Forgetting else block for invalid days
  • Using wrong comparison operators
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard