Bird
0
0

Find the error in this PHP code snippet that tries to skip the outer loop iteration when $j == 2:

medium📝 Debug Q14 of 15
PHP - Loops

Find the error in this PHP code snippet that tries to skip the outer loop iteration when $j == 2:

for ($i = 0; $i < 3; $i++) {
    for ($j = 0; $j < 3; $j++) {
        if ($j == 2) {
            continue 3;
        }
        echo "$i$j ";
    }
}
AThe <code>continue</code> statement should be <code>break 3;</code> instead.
BThere is no error; the code runs fine.
CThe echo statement is inside the wrong loop.
DUsing <code>continue 3;</code> causes a fatal error because only 2 loops exist.
Step-by-Step Solution
Solution:
  1. Step 1: Count the nested loops

    There are only 2 loops: outer ($i) and inner ($j).
  2. Step 2: Check the continue level

    continue 3; tries to skip 3 levels, but only 2 exist, causing a fatal error.
  3. Final Answer:

    Using continue 3; causes a fatal error because only 2 loops exist. -> Option D
  4. Quick Check:

    continue n must not exceed loop depth [OK]
Quick Trick: continue n; must not exceed nested loop count [OK]
Common Mistakes:
  • Using continue with number larger than loop depth
  • Confusing continue with break
  • Ignoring PHP fatal error on invalid continue level

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes