Bird
Raised Fist0
PHPprogramming~20 mins

Continue statement with levels in PHP - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Continue Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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 ";
    }
}
?>
A11 12 13 21 22 23 31 32 33
B11 13 12 13 21 23 31 33
C11 13 21 22 23 31 32 33
D11 21 31
Attempts:
2 left
💡 Hint
Remember that continue 2 skips the rest of the current inner and outer loop iteration.
❓ Predict Output
intermediate
2: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 ";
    }
}
?>
A11 21 31 12 22 32
B11 13 12 13 21 23
C11 13 21 23
D11 12 13 21 22 23
Attempts:
2 left
💡 Hint
continue 1 affects only the current loop level.
🔧 Debug
advanced
2: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++;
    }
}
?>
AOutputs '001 002 101 102 201 202 '
BFatal error: 'continue' not in the correct loop level
CNo output, infinite loop
DSyntax error: unexpected 'continue'
Attempts:
2 left
💡 Hint
Check how many nested loops are present and the level used in continue.
❓ Predict Output
advanced
2: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 ";
    }
}
?>
A0a 1a
B0a 0c 1a 1c
C0a 0c 1a
D0a 1a 1c
Attempts:
2 left
💡 Hint
continue 2 skips the current iteration of the outer loop.
🧠 Conceptual
expert
3: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++;
        }
    }
}
?>
Acontinue 2 skips the rest of the while loop and the current foreach iteration, moving to next foreach value
Bcontinue 2 skips only the current while iteration, continuing the while loop
Ccontinue 2 skips the entire for loop iteration, moving to next x
Dcontinue 2 causes a syntax error because it is inside a while loop
Attempts:
2 left
💡 Hint
Remember that continue with a number skips that many nested loops.

Practice

(1/5)
1.

What does the continue statement do inside a loop in PHP?

easy
A. It skips the rest of the current loop iteration and moves to the next iteration.
B. It stops the entire loop immediately.
C. It restarts the whole program.
D. It pauses the loop for a moment.

Solution

  1. Step 1: Understand the role of continue in loops

    The continue statement tells the loop to skip the remaining code in the current iteration.
  2. Step 2: What happens after continue?

    The loop immediately moves to the next iteration, not stopping the entire loop.
  3. Final Answer:

    It skips the rest of the current loop iteration and moves to the next iteration. -> Option A
  4. Quick Check:

    continue skips current iteration [OK]
Hint: Remember: continue skips current iteration only [OK]
Common Mistakes:
  • Thinking continue stops the whole loop
  • Confusing continue with break
  • Assuming continue pauses the program
2.

Which of the following is the correct syntax to skip the current iteration of the outer loop in nested loops using continue in PHP?

for ($i = 0; $i < 3; $i++) {
    for ($j = 0; $j < 3; $j++) {
        if ($j == 1) {
            // What continue statement skips outer loop iteration?
        }
    }
}
easy
A. continue 2;
B. continue 1;
C. continue;
D. continue 3;

Solution

  1. Step 1: Understand nested loop levels

    There are two loops: outer ($i) and inner ($j). To skip outer loop iteration, we use continue 2;.
  2. Step 2: Apply continue n; syntax

    continue 2; skips the current iteration of the second enclosing loop (outer loop).
  3. Final Answer:

    continue 2; -> Option A
  4. Quick Check:

    continue 2; skips outer loop iteration [OK]
Hint: Use continue n; where n = loop level to skip [OK]
Common Mistakes:
  • Using continue without number skips inner loop only
  • Using continue 1; which skips inner loop only
  • Using continue 3; which is invalid here
3.

What will be the output of the following PHP code?

for ($i = 1; $i <= 3; $i++) {
    for ($j = 1; $j <= 3; $j++) {
        if ($j == 2) {
            continue 2;
        }
        echo "$i$j ";
    }
}
medium
A. 11 13 12 22 32
B. 11 21 31
C. 11 12 13 21 22 23 31 32 33
D. 11 13 21 23 31 33

Solution

  1. Step 1: Analyze the nested loops and continue 2;

    When $j == 2, continue 2; skips the rest of the inner and outer loop iteration, moving to next $i.
  2. Step 2: Trace output for each iteration

    For $i=1: $j=1 prints '11 ', $j=2 triggers continue 2; skips to $i=2.
    For $i=2: $j=1 prints '21 ', $j=2 triggers continue 2; skips to $i=3.
    For $i=3: $j=1 prints '31 ', $j=2 triggers continue 2; loop ends.
  3. Final Answer:

    11 21 31 -> Option B
  4. Quick Check:

    continue 2 skips outer loop iteration [OK]
Hint: continue 2 skips outer loop iteration immediately [OK]
Common Mistakes:
  • Assuming continue 2 skips only inner loop
  • Expecting all combinations printed
  • Ignoring that continue 2 skips outer loop iteration
4.

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 ";
    }
}
medium
A. The continue statement should be break 3; instead.
B. There is no error; the code runs fine.
C. The echo statement is inside the wrong loop.
D. Using continue 3; causes a fatal error because only 2 loops exist.

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]
Hint: 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
5.

You want to print all pairs (i, j) for i and j from 1 to 3, but skip printing pairs where j == 2 and also skip the entire outer loop iteration if i == 2. Which code snippet correctly uses continue with levels to achieve this?

hard
A.
for ($i = 1; $i <= 3; $i++) {
    if ($i == 2) continue;
    for ($j = 1; $j <= 3; $j++) {
        if ($j == 2) continue;
        echo "($i,$j) ";
    }
}
B.
for ($i = 1; $i <= 3; $i++) {
    for ($j = 1; $j <= 3; $j++) {
        if ($j == 2) continue;
        if ($i == 2) continue 2;
        echo "($i,$j) ";
    }
}
C.
for ($i = 1; $i <= 3; $i++) {
    for ($j = 1; $j <= 3; $j++) {
        if ($i == 2) continue 2;
        if ($j == 2) continue;
        echo "($i,$j) ";
    }
}
D.
for ($i = 1; $i <= 3; $i++) {
    for ($j = 1; $j <= 3; $j++) {
        if ($i == 2 || $j == 2) continue 2;
        echo "($i,$j) ";
    }
}

Solution

  1. Step 1: Understand the requirements

    Skip printing pairs where j == 2, and skip entire outer loop iteration if i == 2.
  2. Step 2: Analyze each option

    for ($i = 1; $i <= 3; $i++) {
        if ($i == 2) continue;
        for ($j = 1; $j <= 3; $j++) {
            if ($j == 2) continue;
            echo "($i,$j) ";
        }
    }
    skips i == 2 outer loop correctly but skips j == 2 only in inner loop.
    for ($i = 1; $i <= 3; $i++) {
        for ($j = 1; $j <= 3; $j++) {
            if ($j == 2) continue;
            if ($i == 2) continue 2;
            echo "($i,$j) ";
        }
    }
    tries to continue 2 inside inner loop after j check, but order is wrong.
    for ($i = 1; $i <= 3; $i++) {
        for ($j = 1; $j <= 3; $j++) {
            if ($i == 2) continue 2;
            if ($j == 2) continue;
            echo "($i,$j) ";
        }
    }
    first skips outer loop iteration if i == 2 using continue 2;, then skips j == 2 in inner loop.
    for ($i = 1; $i <= 3; $i++) {
        for ($j = 1; $j <= 3; $j++) {
            if ($i == 2 || $j == 2) continue 2;
            echo "($i,$j) ";
        }
    }
    skips outer loop iteration if either i == 2 or j == 2, which is incorrect.
  3. Final Answer:

    Option C correctly uses continue 2; to skip outer loop when i == 2 and continue to skip j == 2. -> Option C
  4. Quick Check:

    Use continue 2; to skip outer loop inside inner loop [OK]
Hint: Place continue 2; before continue for inner loop skip [OK]
Common Mistakes:
  • Skipping outer loop for wrong conditions
  • Using continue 2; incorrectly for inner loop skip
  • Combining conditions incorrectly causing wrong skips
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard