Continue statement with levels in PHP - Time & Space Complexity
Let's see how using the continue statement with levels affects how many times parts of the code run.
We want to know how skipping certain loops changes the total work done.
Analyze the time complexity of the following code snippet.
for ($i = 0; $i < $n; $i++) {
for ($j = 0; $j < $n; $j++) {
if ($j % 2 == 0) {
continue 2; // skips to next $i iteration
}
// some constant time operation
}
}
This code loops over two levels. When $j is even, it skips the rest of inner and outer loop body and moves to the next $i.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Nested loops over $i and $j.
- How many times: Outer loop runs $n times; inner loop runs partially due to continue 2.
Because continue 2 skips inner and outer loop when $j is even, inner loop does very little work per outer loop.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 5 (one inner iteration per outer loop when $j is odd) |
| 100 | 50 |
| 1000 | 500 |
Pattern observation: Operations grow roughly linearly with input size, not squared.
Time Complexity: O(n)
This means the total work grows in direct proportion to the input size.
[X] Wrong: "Since there are two loops, the time must be O(n²)."
[OK] Correct: The continue 2 skips many inner loop steps, so the inner loop does not fully run each time.
Understanding how control statements like continue with levels affect loops helps you explain code efficiency clearly and confidently.
"What if we changed continue 2 to continue 1? How would the time complexity change?"
Practice
What does the continue statement do inside a loop in PHP?
Solution
Step 1: Understand the role of
Thecontinuein loopscontinuestatement tells the loop to skip the remaining code in the current iteration.Step 2: What happens after
The loop immediately moves to the next iteration, not stopping the entire loop.continue?Final Answer:
It skips the rest of the current loop iteration and moves to the next iteration. -> Option AQuick Check:
continueskips current iteration [OK]
- Thinking continue stops the whole loop
- Confusing continue with break
- Assuming continue pauses the program
Which of the following is the correct syntax to skip the current iteration of the outer loop in nested loops using continue in PHP?
for ($i = 0; $i < 3; $i++) {
for ($j = 0; $j < 3; $j++) {
if ($j == 1) {
// What continue statement skips outer loop iteration?
}
}
}Solution
Step 1: Understand nested loop levels
There are two loops: outer ($i) and inner ($j). To skip outer loop iteration, we usecontinue 2;.Step 2: Apply
continue n;syntaxcontinue 2;skips the current iteration of the second enclosing loop (outer loop).Final Answer:
continue 2; -> Option AQuick Check:
continue 2;skips outer loop iteration [OK]
- Using continue without number skips inner loop only
- Using continue 1; which skips inner loop only
- Using continue 3; which is invalid here
What will be the output of the following PHP code?
for ($i = 1; $i <= 3; $i++) {
for ($j = 1; $j <= 3; $j++) {
if ($j == 2) {
continue 2;
}
echo "$i$j ";
}
}Solution
Step 1: Analyze the nested loops and continue 2;
When $j == 2,continue 2;skips the rest of the inner and outer loop iteration, moving to next $i.Step 2: Trace output for each iteration
For $i=1: $j=1 prints '11 ', $j=2 triggers continue 2; skips to $i=2.
For $i=2: $j=1 prints '21 ', $j=2 triggers continue 2; skips to $i=3.
For $i=3: $j=1 prints '31 ', $j=2 triggers continue 2; loop ends.Final Answer:
11 21 31 -> Option BQuick Check:
continue 2 skips outer loop iteration [OK]
- Assuming continue 2 skips only inner loop
- Expecting all combinations printed
- Ignoring that continue 2 skips outer loop iteration
Find the error in this PHP code snippet that tries to skip the outer loop iteration when $j == 2:
for ($i = 0; $i < 3; $i++) {
for ($j = 0; $j < 3; $j++) {
if ($j == 2) {
continue 3;
}
echo "$i$j ";
}
}Solution
Step 1: Count the nested loops
There are only 2 loops: outer ($i) and inner ($j).Step 2: Check the continue level
continue 3;tries to skip 3 levels, but only 2 exist, causing a fatal error.Final Answer:
Using continue 3; causes a fatal error because only 2 loops exist. -> Option DQuick Check:
continue n must not exceed loop depth [OK]
- Using continue with number larger than loop depth
- Confusing continue with break
- Ignoring PHP fatal error on invalid continue level
You want to print all pairs (i, j) for i and j from 1 to 3, but skip printing pairs where j == 2 and also skip the entire outer loop iteration if i == 2. Which code snippet correctly uses continue with levels to achieve this?
Solution
Step 1: Understand the requirements
Skip printing pairs where j == 2, and skip entire outer loop iteration if i == 2.Step 2: Analyze each option
skips i == 2 outer loop correctly but skips j == 2 only in inner loop.for ($i = 1; $i <= 3; $i++) { if ($i == 2) continue; for ($j = 1; $j <= 3; $j++) { if ($j == 2) continue; echo "($i,$j) "; } }
tries to continue 2 inside inner loop after j check, but order is wrong.for ($i = 1; $i <= 3; $i++) { for ($j = 1; $j <= 3; $j++) { if ($j == 2) continue; if ($i == 2) continue 2; echo "($i,$j) "; } }
first skips outer loop iteration if i == 2 usingfor ($i = 1; $i <= 3; $i++) { for ($j = 1; $j <= 3; $j++) { if ($i == 2) continue 2; if ($j == 2) continue; echo "($i,$j) "; } }continue 2;, then skips j == 2 in inner loop.
skips outer loop iteration if either i == 2 or j == 2, which is incorrect.for ($i = 1; $i <= 3; $i++) { for ($j = 1; $j <= 3; $j++) { if ($i == 2 || $j == 2) continue 2; echo "($i,$j) "; } }Final Answer:
Option C correctly uses continue 2; to skip outer loop when i == 2 and continue to skip j == 2. -> Option CQuick Check:
Use continue 2; to skip outer loop inside inner loop [OK]
- Skipping outer loop for wrong conditions
- Using continue 2; incorrectly for inner loop skip
- Combining conditions incorrectly causing wrong skips
Ready to prove it?
Pass the test and this pattern moves unknown
