0
0
PHPprogramming~10 mins

Continue statement with levels in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Continue statement with levels
Start Outer Loop
Start Inner Loop
Check Continue Level
Continue Inner Loop
Continue Outer Loop
Execute Remaining Inner Loop Code
End Inner Loop
End Outer Loop
The continue statement with levels skips the rest of the current loop iteration at the specified loop level, either inner or outer.
Execution Sample
PHP
<?php
for ($i = 1; $i <= 2; $i++) {
  for ($j = 1; $j <= 3; $j++) {
    if ($j == 2) continue 2;
    echo "$i$j ";
  }
}
?>
This code uses continue 2 to skip to the next iteration of the outer loop when inner loop variable $j equals 2.
Execution Table
StepijCondition (j==2)ActionOutput
111FalsePrint '11 '11
212Truecontinue 2: skip to next i
321FalsePrint '21 '21
422Truecontinue 2: skip to next i
53--i=3 > 2 exit outer loop
💡 Outer loop ends when i becomes 3, which is greater than 2.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
i112233
j12212-
Key Moments - 2 Insights
Why does the output skip printing when j equals 2?
Because at step 2 and 4 in the execution_table, the condition j==2 is true and continue 2 skips the rest of both inner and outer loop iteration, so echo is not executed.
What does 'continue 2' mean in this nested loop?
'continue 2' means skip the rest of the current iteration of the outer loop, not just the inner loop. This is shown in steps 2 and 4 where it jumps to the next i.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of i at step 3?
A1
B3
C2
D0
💡 Hint
Check the 'i' column in the execution_table row for step 3.
At which step does the condition j==2 first become true?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Condition (j==2)' column in the execution_table.
If we change 'continue 2' to 'continue 1', what would happen at step 2?
ASkip only inner loop iteration, continue outer loop normally
BSkip outer loop iteration like before
CProgram will error
DNo change in output
💡 Hint
Recall that 'continue 1' affects only the innermost loop, unlike 'continue 2'.
Concept Snapshot
continue [n] skips the rest of the current loop iteration at level n.
In nested loops, continue 1 affects inner loop, continue 2 affects outer loop.
Use to control flow precisely in nested loops.
Syntax: continue; or continue 2;
Skipping outer loop iteration can save unnecessary inner loops.
Full Transcript
This example shows how the continue statement with levels works in PHP nested loops. The outer loop runs with variable i from 1 to 2. The inner loop runs with j from 1 to 3. When j equals 2, the code uses continue 2 to skip the rest of the current iteration of the outer loop. This means it stops the inner loop and moves to the next i value. The output prints only when j is not 2. The execution table traces each step, showing variable values, condition checks, actions, and output. The variable tracker shows how i and j change after each step. Key moments clarify why output is skipped and what continue 2 means. The quiz tests understanding of variable values and continue behavior. The snapshot summarizes the concept for quick review.