Challenge - 5 Problems
Continue Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of nested loops with continue 2
What is the output of this PHP code snippet?
PHP
<?php for ($i = 1; $i <= 3; $i++) { for ($j = 1; $j <= 3; $j++) { if ($j == 2) { continue 2; } echo "$i$j "; } } ?>
Attempts:
2 left
💡 Hint
Remember that continue 2 skips the rest of the current inner and outer loop iteration.
✗ Incorrect
The continue 2 statement skips the rest of the inner loop and also the current iteration of the outer loop. So when j == 2, it skips to the next i value, not printing anything for j == 2.
❓ Predict Output
intermediate2:00remaining
Effect of continue 1 in nested loops
What will this PHP code output?
PHP
<?php for ($x = 1; $x <= 2; $x++) { for ($y = 1; $y <= 3; $y++) { if ($y == 2) { continue 1; } echo "$x$y "; } } ?>
Attempts:
2 left
💡 Hint
continue 1 affects only the current loop level.
✗ Incorrect
continue 1 skips the current iteration of the inner loop only, so when y == 2, it skips printing and continues inner loop with next y.
🔧 Debug
advanced2:00remaining
Identify the error in continue statement usage
What error will this PHP code produce?
PHP
<?php for ($a = 0; $a < 3; $a++) { $b = 0; while ($b < 3) { if ($b == 1) { continue 3; } echo "$a$b "; $b++; } } ?>
Attempts:
2 left
💡 Hint
Check how many nested loops are present and the level used in continue.
✗ Incorrect
continue 3 tries to skip 3 loop levels but only 2 loops exist, causing a fatal error.
❓ Predict Output
advanced2:00remaining
Output with continue 2 inside foreach and for loops
What is the output of this PHP code?
PHP
<?php $items = ['a', 'b', 'c']; for ($i = 0; $i < 2; $i++) { foreach ($items as $item) { if ($item === 'b') { continue 2; } echo "$i$item "; } } ?>
Attempts:
2 left
💡 Hint
continue 2 skips the current iteration of the outer loop.
✗ Incorrect
When item is 'b', continue 2 skips the rest of foreach and also the current for iteration, so 'c' is never printed for that i.
🧠 Conceptual
expert3:00remaining
Understanding continue with levels in complex nested loops
Consider this PHP code with three nested loops. Which statement about the effect of
continue 2; inside the innermost loop is true?PHP
<?php for ($x = 1; $x <= 2; $x++) { foreach ([10, 20] as $y) { $z = 0; while ($z < 3) { if ($z == 1) { continue 2; } echo "$x$y$z "; $z++; } } } ?>
Attempts:
2 left
💡 Hint
Remember that continue with a number skips that many nested loops.
✗ Incorrect
continue 2 skips the current and next outer loop. Here, inside while, continue 2 skips while and foreach iteration, moving to next foreach value.