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 multiple conditions
What is the output of the following C code?
C
#include <stdio.h> int main() { int x = 15; if (x < 10) { printf("Less than 10\n"); } else if (x < 20) { printf("Between 10 and 19\n"); } else if (x < 30) { printf("Between 20 and 29\n"); } else { printf("30 or more\n"); } return 0; }
Attempts:
2 left
💡 Hint
Check which condition matches first in the else-if ladder.
✗ Incorrect
The variable x is 15, which is not less than 10, so the first if fails. The second condition checks if x < 20, which is true, so it prints "Between 10 and 19" and skips the rest.
❓ Predict Output
intermediate2:00remaining
Output when all else-if conditions fail
What will this C program print?
C
#include <stdio.h> int main() { int num = 50; if (num < 10) { printf("Small number\n"); } else if (num < 20) { printf("Medium number\n"); } else if (num < 30) { printf("Large number\n"); } else { printf("Extra large number\n"); } return 0; }
Attempts:
2 left
💡 Hint
Check which condition matches the value 50.
✗ Incorrect
The value 50 does not satisfy any of the first three conditions, so the else block runs and prints "Extra large number".
🔧 Debug
advanced2:00remaining
Identify the error in else-if ladder syntax
What error does this C code produce when compiled?
C
#include <stdio.h> int main() { int a = 5; if (a > 0) { printf("Positive\n"); } else if a == 0 { printf("Zero\n"); } else { printf("Negative\n"); } return 0; }
Attempts:
2 left
💡 Hint
Check the syntax of the else-if condition.
✗ Incorrect
In C, the condition in else-if must be enclosed in parentheses. The code 'else if a == 0' is missing parentheses, causing a syntax error.
❓ Predict Output
advanced2:00remaining
Output of nested else-if ladder with multiple variables
What is the output of this C program?
C
#include <stdio.h> int main() { int x = 8, y = 12; if (x > 10) { if (y > 10) { printf("x > 10 and y > 10\n"); } else { printf("x > 10 and y <= 10\n"); } } else if (x > 5) { printf("x between 6 and 10\n"); } else { printf("x 5 or less\n"); } return 0; }
Attempts:
2 left
💡 Hint
Check the first condition and then the else-if condition carefully.
✗ Incorrect
x is 8, which is not greater than 10, so the first if block is skipped. The else-if checks if x > 5, which is true, so it prints "x between 6 and 10".
🧠 Conceptual
expert2:00remaining
Number of times conditions are evaluated in else-if ladder
Consider this else-if ladder in C:
if (cond1) {
// block1
} else if (cond2) {
// block2
} else if (cond3) {
// block3
} else {
// block4
}
If cond1 is false, cond2 is true, and cond3 is true, how many conditions are evaluated before executing a block?Attempts:
2 left
💡 Hint
Remember else-if stops checking after the first true condition.
✗ Incorrect
The program checks cond1 (false), then cond2 (true). Since cond2 is true, it executes block2 and stops. So only 2 conditions are evaluated.