Challenge - 5 Problems
If Statement Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of nested if statements
What is the output of this C++ code snippet?
C++
int x = 5; int y = 10; if (x > 3) { if (y < 15) { std::cout << "A"; } else { std::cout << "B"; } } else { std::cout << "C"; }
Attempts:
2 left
💡 Hint
Check both conditions in the nested if statements carefully.
✗ Incorrect
Since x is 5 (which is greater than 3) and y is 10 (which is less than 15), the first if and the nested if both run, printing 'A'.
❓ Predict Output
intermediate2:00remaining
If-else with logical operators
What will this C++ code print?
C++
int a = 7; int b = 3; if (a > 5 && b < 5) { std::cout << "X"; } else { std::cout << "Y"; }
Attempts:
2 left
💡 Hint
Both conditions in the if must be true for the if block to run.
✗ Incorrect
a is 7 (greater than 5) and b is 3 (less than 5), so both conditions are true and 'X' is printed.
❓ Predict Output
advanced2:00remaining
If statement with assignment inside condition
What is the output of this C++ code?
C++
int x = 0; if (x = 5) { std::cout << x; } else { std::cout << 0; }
Attempts:
2 left
💡 Hint
Remember that assignment inside if condition returns the assigned value.
✗ Incorrect
The assignment 'x = 5' sets x to 5 and returns 5, which is true in the if condition, so it prints 5.
❓ Predict Output
advanced2:00remaining
If statement with multiple else if branches
What will this C++ code print?
C++
int n = 15; if (n < 10) { std::cout << "Low"; } else if (n < 20) { std::cout << "Medium"; } else { std::cout << "High"; }
Attempts:
2 left
💡 Hint
Check which condition matches the value of n first.
✗ Incorrect
n is 15, which is not less than 10 but less than 20, so it prints 'Medium'.
❓ Predict Output
expert2:00remaining
If statement with complex boolean expression
What is the output of this C++ code?
C++
bool a = true; bool b = false; bool c = true; if ((a && b) || (b || c)) { std::cout << "Yes"; } else { std::cout << "No"; }
Attempts:
2 left
💡 Hint
Evaluate the boolean expression step by step.
✗ Incorrect
The expression (a && b) is false, but (b || c) is true, so the whole condition is true and prints 'Yes'.