Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using break 2 breaks out of both loops, not just the inner one.
✗ Incorrect
The break statement without a number breaks out of the innermost loop only.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using break without a number only exits the inner loop.
✗ Incorrect
The break 2 statement breaks out of two nested loops.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using break 2 only exits two loops, not three.
✗ Incorrect
To break out of three nested loops, use break 3.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using break without a number exits only the inner loop.
Printing $j instead of $i after the loops.
✗ Incorrect
break 2 exits both loops when $j equals 2, then echo $i; prints the current value of $i.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using break without a number exits only the inner loop.
Mixing up which variables to print.
✗ Incorrect
break 2 exits both loops when $y equals 3, then echo $x + $y; prints their sum.