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 blocks
What is the output of this PHP code snippet?
PHP
<?php $score = 75; if ($score >= 90) { echo "Grade: A"; } else { if ($score >= 70) { echo "Grade: B"; } else { echo "Grade: C"; } } ?>
Attempts:
2 left
💡 Hint
Check the first condition and then the nested condition inside the else block.
✗ Incorrect
The score 75 is not >= 90, so the first if fails. Then the nested if checks if score >= 70, which is true, so it prints 'Grade: B'.
❓ Predict Output
intermediate2:00remaining
Nested condition with logical operators
What will this PHP code output?
PHP
<?php $age = 20; $hasID = true; if ($age >= 18) { if ($hasID) { echo "Access granted"; } else { echo "ID required"; } } else { echo "Access denied"; } ?>
Attempts:
2 left
💡 Hint
Check both conditions inside the nested if.
✗ Incorrect
Age is 20 (>=18) and hasID is true, so both conditions pass and 'Access granted' is printed.
🔧 Debug
advanced2:00remaining
Identify the error in nested condition
What error does this PHP code produce when run?
PHP
<?php $number = 10; if ($number > 0) { if $number < 20 { echo "Number is between 1 and 19"; } } ?>
Attempts:
2 left
💡 Hint
Check the syntax of the nested if statement.
✗ Incorrect
The nested if is missing parentheses around the condition, causing a syntax error.
❓ Predict Output
advanced2:00remaining
Output of nested if-elseif-else
What is the output of this PHP code?
PHP
<?php $val = 15; if ($val > 20) { echo "Greater than 20"; } elseif ($val > 10) { if ($val % 2 == 0) { echo "Even and greater than 10"; } else { echo "Odd and greater than 10"; } } else { echo "10 or less"; } ?>
Attempts:
2 left
💡 Hint
Check the elseif condition and then the nested if inside it.
✗ Incorrect
15 is not > 20, but is > 10. 15 % 2 is 1 (odd), so it prints 'Odd and greater than 10'.
🧠 Conceptual
expert3:00remaining
Understanding nested conditional flow
Consider this PHP code snippet. What will be the value of $result after execution?
PHP
<?php $a = 5; $b = 10; if ($a > $b) { if ($a - $b > 3) { $result = 'A'; } else { $result = 'B'; } } else { if ($b - $a > 3) { $result = 'C'; } else { $result = 'D'; } } ?>
Attempts:
2 left
💡 Hint
Compare values of $a and $b and check the nested conditions carefully.
✗ Incorrect
Since $a (5) is not greater than $b (10), the else block runs. Then $b - $a = 5, which is > 3, so $result is set to 'C'.