Challenge - 5 Problems
Nested Loop Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of nested for loops with break
What is the output of this PHP code?
PHP
<?php for ($i = 1; $i <= 3; $i++) { for ($j = 1; $j <= 3; $j++) { if ($j == 2) { break; } echo "$i$j "; } } ?>
Attempts:
2 left
💡 Hint
Remember that break exits the inner loop immediately when condition is met.
✗ Incorrect
The inner loop breaks when $j == 2, so only $j == 1 is printed for each $i. So output is 11 21 31.
❓ Predict Output
intermediate2:00remaining
Output of nested while loops with continue
What is the output of this PHP code?
PHP
<?php $i = 1; while ($i <= 2) { $j = 1; while ($j <= 3) { if ($j == 2) { $j++; continue; } echo "$i$j "; $j++; } $i++; } ?>
Attempts:
2 left
💡 Hint
The continue skips printing when $j == 2 but increments $j before continuing.
✗ Incorrect
When $j == 2, it increments $j and continues, skipping echo. So 12 and 22 are skipped.
🧠 Conceptual
advanced2:00remaining
Number of iterations in nested loops
How many times will the innermost statement execute in this PHP code?
PHP
<?php for ($a = 0; $a < 4; $a++) { for ($b = 0; $b < 3; $b++) { for ($c = 0; $c < 2; $c++) { // innermost statement } } } ?>
Attempts:
2 left
💡 Hint
Multiply the number of iterations of each loop.
✗ Incorrect
4 * 3 * 2 = 24 times the innermost statement runs.
🔧 Debug
advanced2:00remaining
Identify the error in nested loops
What error will this PHP code produce?
PHP
<?php for ($x = 0; $x < 3; $x++) { for ($y = 0; $y < 3; $y++) echo "$x$y "; } ?>
Attempts:
2 left
💡 Hint
Check the braces carefully for matching pairs.
✗ Incorrect
The inner for loop lacks braces but the closing brace after echo is extra, causing syntax error.
🚀 Application
expert3:00remaining
Output of nested loops with conditional break and continue
What is the output of this PHP code?
PHP
<?php for ($i = 1; $i <= 3; $i++) { for ($j = 1; $j <= 3; $j++) { if ($i == $j) { continue; } if ($j == 3) { break; } echo "$i$j "; } } ?>
Attempts:
2 left
💡 Hint
Remember continue skips current iteration, break exits inner loop.
✗ Incorrect
When $i == $j, continue skips printing. When $j == 3, break exits inner loop. The output is 12 21 31 32.