Bird
0
0

How can you use break with levels to exit nested loops and also execute code after the loops?

hard📝 Application Q9 of 15
PHP - Loops

How can you use break with levels to exit nested loops and also execute code after the loops?

for ($a = 0; $a < 3; $a++) {
    for ($b = 0; $b < 3; $b++) {
        if ($b == 1) {
            break 2;
        }
        echo "$a$b ";
    }
}
echo "Done";
Aexit; stops script immediately, no code after runs.
Bbreak; exits only inner loop and skips after loops.
Ccontinue 2; skips current iteration of both loops.
Dbreak 2; exits both loops and continues after loops.
Step-by-Step Solution
Solution:
  1. Step 1: Understand break 2;

    Using break 2; exits both inner and outer loops immediately.
  2. Step 2: Code after loops runs

    After breaking out of loops, the code echo "Done"; runs normally.
  3. Final Answer:

    break 2; exits both loops and continues after loops. -> Option D
  4. Quick Check:

    Break with levels exits loops, code after runs [OK]
Quick Trick: Break n exits loops, code after loops runs normally [OK]
Common Mistakes:
  • Thinking break skips code after loops
  • Confusing continue with break
  • Using exit stops entire script

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes