0
0
PHPprogramming~10 mins

Break statement with levels in PHP - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to break out of the inner loop only.

PHP
<?php
for ($i = 0; $i < 3; $i++) {
    for ($j = 0; $j < 3; $j++) {
        if ($j == 1) {
            [1];
        }
        echo "$i$j ";
    }
    echo "\n";
}
?>
Drag options to blanks, or click blank then click option'
Abreak
Bexit
Ccontinue
Dbreak 2
Attempts:
3 left
💡 Hint
Common Mistakes
Using break 2 breaks out of both loops, not just the inner one.
2fill in blank
medium

Complete the code to break out of both loops when $j equals 1.

PHP
<?php
for ($i = 0; $i < 3; $i++) {
    for ($j = 0; $j < 3; $j++) {
        if ($j == 1) {
            [1];
        }
        echo "$i$j ";
    }
    echo "\n";
}
?>
Drag options to blanks, or click blank then click option'
Abreak 2
Bbreak
Ccontinue 2
Dexit
Attempts:
3 left
💡 Hint
Common Mistakes
Using break without a number only exits the inner loop.
3fill in blank
hard

Fix the error in the break statement to exit three nested loops.

PHP
<?php
for ($a = 0; $a < 2; $a++) {
    for ($b = 0; $b < 2; $b++) {
        for ($c = 0; $c < 2; $c++) {
            if ($c == 1) {
                [1];
            }
            echo "$a$b$c ";
        }
    }
    echo "\n";
}
?>
Drag options to blanks, or click blank then click option'
Abreak 2
Bbreak 3
Cbreak
Dcontinue 3
Attempts:
3 left
💡 Hint
Common Mistakes
Using break 2 only exits two loops, not three.
4fill in blank
hard

Fill both blanks to break out of the correct number of loops and print the last value of $i.

PHP
<?php
for ($i = 0; $i < 5; $i++) {
    for ($j = 0; $j < 5; $j++) {
        if ($j == 2) {
            [1];
        }
    }
}
echo $[2];
?>
Drag options to blanks, or click blank then click option'
Abreak 2
Bbreak
Ci
Dj
Attempts:
3 left
💡 Hint
Common Mistakes
Using break without a number exits only the inner loop.
Printing $j instead of $i after the loops.
5fill in blank
hard

Fill all three blanks to break out of the correct loops and print the sum of $x and $y.

PHP
<?php
for ($x = 0; $x < 4; $x++) {
    for ($y = 0; $y < 4; $y++) {
        if ($y == 3) {
            [1];
        }
        echo "$x$y ";
    }
}
echo [2] + [3];
?>
Drag options to blanks, or click blank then click option'
Abreak 2
Bx
Cy
Dbreak
Attempts:
3 left
💡 Hint
Common Mistakes
Using break without a number exits only the inner loop.
Mixing up which variables to print.