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 else-if ladder with multiple true conditions
What will be the output of the following C# code?
C Sharp (C#)
int x = 15; if (x > 10) { Console.WriteLine("Greater than 10"); } else if (x > 5) { Console.WriteLine("Greater than 5"); } else { Console.WriteLine("5 or less"); }
Attempts:
2 left
💡 Hint
Remember, in an else-if ladder, only the first true condition's block runs.
✗ Incorrect
Since x is 15, the first condition (x > 10) is true, so it prints "Greater than 10" and skips the rest.
❓ Predict Output
intermediate2:00remaining
Output when no conditions in else-if ladder are true
What will be the output of this C# code snippet?
C Sharp (C#)
int y = 3; if (y > 10) { Console.WriteLine("Greater than 10"); } else if (y > 5) { Console.WriteLine("Greater than 5"); } else { Console.WriteLine("5 or less"); }
Attempts:
2 left
💡 Hint
Check which conditions are false and what the else block does.
✗ Incorrect
Since y is 3, both if and else-if conditions are false, so the else block runs printing "5 or less".
🔧 Debug
advanced2:00remaining
Identify the error in else-if ladder syntax
Which option contains a syntax error in the else-if ladder?
C Sharp (C#)
int z = 7; if (z > 10) { Console.WriteLine("Greater than 10"); } else if (z > 5) { Console.WriteLine("Greater than 5"); } else { Console.WriteLine("5 or less"); }
Attempts:
2 left
💡 Hint
Check the syntax of the else-if condition carefully.
✗ Incorrect
Option A is missing parentheses around the else-if condition, causing a syntax error.
🚀 Application
advanced2:00remaining
Determine the final value after else-if ladder execution
What is the value of variable result after running this code?
C Sharp (C#)
int score = 85; string result; if (score >= 90) { result = "Excellent"; } else if (score >= 75) { result = "Good"; } else if (score >= 60) { result = "Pass"; } else { result = "Fail"; }
Attempts:
2 left
💡 Hint
Check which condition matches the score 85 first.
✗ Incorrect
85 is not >= 90, so first if is false. 85 >= 75 is true, so result is set to "Good" and ladder stops.
🧠 Conceptual
expert2:00remaining
Understanding else-if ladder execution flow
Consider this else-if ladder. Which statement best describes how it executes?
C Sharp (C#)
int n = 20; if (n < 10) { Console.WriteLine("Less than 10"); } else if (n < 20) { Console.WriteLine("Less than 20"); } else if (n == 20) { Console.WriteLine("Equal to 20"); } else { Console.WriteLine("Greater than 20"); }
Attempts:
2 left
💡 Hint
Think about how else-if ladder controls flow after a true condition.
✗ Incorrect
In an else-if ladder, once a true condition is found, its block runs and the rest are skipped.