Challenge - 5 Problems
Else-if Ladder Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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; }
Attempts:
2 left
💡 Hint
Check which condition is true first in the else-if ladder.
✗ Incorrect
The variable num is 15. The first condition (num < 10) is false. The second condition (num < 20) is true, so it prints "Between 10 and 19" and skips the rest.
❓ Predict Output
intermediate2: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; }
Attempts:
2 left
💡 Hint
Check the conditions from top to bottom carefully.
✗ Incorrect
score is 85. The first condition (score >= 90) is false. The second condition (score >= 80) is true, so it prints "Grade B".
🔧 Debug
advanced2: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; }
Attempts:
2 left
💡 Hint
Check the syntax of the else-if statement carefully.
✗ Incorrect
In C++, the condition in else if must be enclosed in parentheses. The code has else if x == 0 without parentheses, causing a syntax error.
🧠 Conceptual
advanced2: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 }
Attempts:
2 left
💡 Hint
Think about how else-if ladder chooses which block to run.
✗ Incorrect
In an else-if ladder, only the first true condition's block runs. The rest are skipped.
❓ Predict Output
expert2: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; }
Attempts:
2 left
💡 Hint
Check each condition carefully in order.
✗ Incorrect
x=10, y=20. First condition (x > y) is false. Second (x == y) is false. Third (y - x == 10) is true, so it prints "Difference is 10".