0
0
PHPprogramming~20 mins

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

Choose your learning style9 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.