Challenge - 5 Problems
Nested Condition Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ Predict Output
intermediate2:00remaining
Output of nested if-else with integer input
What is the output of this C++ code when
num = 15?C++
#include <iostream> using namespace std; int main() { int num = 15; if (num > 10) { if (num < 20) { cout << "Between 11 and 19" << endl; } else { cout << "20 or more" << endl; } } else { cout << "10 or less" << endl; } return 0; }
Attempts:
2 left
๐ก Hint
Check the conditions step by step: first if num > 10, then if num < 20.
โ Incorrect
The number 15 is greater than 10 and less than 20, so the inner if condition is true, printing 'Between 11 and 19'.
โ Predict Output
intermediate2:00remaining
Value of variable after nested conditions
What is the value of
result after running this code?C++
int x = 5; int y = 10; int result = 0; if (x > 3) { if (y < 5) { result = 1; } else { result = 2; } } else { result = 3; }
Attempts:
2 left
๐ก Hint
Check the conditions for x and y carefully.
โ Incorrect
x is 5 which is greater than 3, so the outer if is true. y is 10 which is not less than 5, so the else branch sets result to 2.
๐ง Debug
advanced2:00remaining
Identify the error in nested if-else code
What error will this code produce when compiled?
C++
#include <iostream> using namespace std; int main() { int a = 7; if (a > 5) if (a < 10) cout << "a is between 6 and 9" << endl; else cout << "a is 10 or more" << endl; else cout << "a is 5 or less" << endl; return 0; }
Attempts:
2 left
๐ก Hint
The second 'else' has no matching 'if' because the first 'else' already pairs with the inner 'if'.
โ Incorrect
No compilation error. The 'else' pairs with the nearest unmatched 'if'. The code compiles and outputs 'a is between 6 and 9' because a=7 satisfies both conditions.
๐ Syntax
advanced2:00remaining
Find the syntax error in nested if-else
Which option contains the correct nested if-else syntax?
Attempts:
2 left
๐ก Hint
Check for balanced braces and proper else matching.
โ Incorrect
Option B uses braces properly for both if and else blocks, ensuring correct syntax and clear structure.
๐ Application
expert3:00remaining
Determine output of complex nested conditions
What will be printed when this code runs?
C++
#include <iostream> using namespace std; int main() { int score = 85; if (score >= 90) { cout << "Grade A" << endl; } else { if (score >= 80) { if (score >= 85) { cout << "Grade B+" << endl; } else { cout << "Grade B" << endl; } } else { cout << "Grade C or below" << endl; } } return 0; }
Attempts:
2 left
๐ก Hint
Follow the nested conditions carefully for score 85.
โ Incorrect
Score 85 is not >= 90, so outer else runs. Then score >= 80 is true, then score >= 85 is true, so prints 'Grade B+'.