The continue statement skips the rest of the current loop iteration and moves to the next iteration.
Click to reveal answer
intermediate
What is the purpose of using continue with a numeric argument (levels) in PHP?
Using continue with a number tells PHP to skip the rest of the current iteration of an outer loop that many levels up.
Click to reveal answer
intermediate
How does continue 2; behave inside nested loops?
continue 2; skips the rest of the current iteration of the inner loop and also skips the rest of the current iteration of the outer loop, moving to the next iteration of the outer loop.
Click to reveal answer
beginner
Can continue be used outside of loops in PHP?
No, continue can only be used inside looping structures like for, foreach, while, and do-while loops.
Click to reveal answer
advanced
What happens if you use continue with a level number greater than the number of nested loops?
PHP will generate a warning and treat it as a normal continue (level 1), skipping only the current loop iteration.
Click to reveal answer
What does continue; do inside a loop?
AExits the loop completely
BRestarts the entire program
CSkips the rest of the current iteration and moves to the next iteration
DPauses the loop
✗ Incorrect
continue; skips the rest of the current loop iteration and continues with the next iteration.
What does continue 2; do inside two nested loops?
AExits both loops completely
BSkips the current iteration of the outer loop only
CSkips the current iteration of the inner loop only
DSkips the current iteration of both inner and outer loops
✗ Incorrect
continue 2; skips the rest of the current iteration of the inner loop and also the outer loop, moving to the next iteration of the outer loop.
What happens if you use continue 3; inside two nested loops?
APHP ignores the statement
BPHP treats it as <code>continue 1;</code> and skips only the inner loop iteration
CPHP throws a fatal error
DPHP skips three levels of loops
✗ Incorrect
Using a level greater than the number of nested loops causes PHP to treat it as continue 1; and issue a warning.
Can continue be used inside a switch statement in PHP?
ANo, <code>continue</code> is for loops only
BNo, it causes a syntax error
CYes, but only with levels
DYes, it skips to the next case
✗ Incorrect
continue is designed for loops, not switch statements.
Which of the following is a correct use of continue with levels?
AInside nested loops to skip outer loop iterations
BTo exit a function early
CTo pause execution for a few seconds
DTo restart the entire script
✗ Incorrect
continue with levels is used inside nested loops to skip iterations of outer loops.
Explain how the continue statement with a numeric level works inside nested loops in PHP.
Think about how many loops you want to skip ahead in.
You got /4 concepts.
Describe what happens if you use a continue level number greater than the number of nested loops.
Consider PHP's behavior when the level is too high.
You got /4 concepts.
Practice
(1/5)
1.
What does the continue statement do inside a loop in PHP?
easy
A. It skips the rest of the current loop iteration and moves to the next iteration.
B. It stops the entire loop immediately.
C. It restarts the whole program.
D. It pauses the loop for a moment.
Solution
Step 1: Understand the role of continue in loops
The continue statement tells the loop to skip the remaining code in the current iteration.
Step 2: What happens after continue?
The loop immediately moves to the next iteration, not stopping the entire loop.
Final Answer:
It skips the rest of the current loop iteration and moves to the next iteration. -> Option A
Quick Check:
continue skips current iteration [OK]
Hint: Remember: continue skips current iteration only [OK]
Common Mistakes:
Thinking continue stops the whole loop
Confusing continue with break
Assuming continue pauses the program
2.
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?
}
}
}
easy
A. continue 2;
B. continue 1;
C. continue;
D. continue 3;
Solution
Step 1: Understand nested loop levels
There are two loops: outer ($i) and inner ($j). To skip outer loop iteration, we use continue 2;.
Step 2: Apply continue n; syntax
continue 2; skips the current iteration of the second enclosing loop (outer loop).
Final Answer:
continue 2; -> Option A
Quick Check:
continue 2; skips outer loop iteration [OK]
Hint: Use continue n; where n = loop level to skip [OK]
Common Mistakes:
Using continue without number skips inner loop only
Using continue 1; which skips inner loop only
Using continue 3; which is invalid here
3.
What will be the output of the following PHP code?
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.
A. The continue statement should be break 3; instead.
B. There is no error; the code runs fine.
C. The echo statement is inside the wrong loop.
D. Using continue 3; causes a fatal error because only 2 loops exist.
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 D
Quick Check:
continue n must not exceed loop depth [OK]
Hint: continue n; must not exceed nested loop count [OK]
Common Mistakes:
Using continue with number larger than loop depth
Confusing continue with break
Ignoring PHP fatal error on invalid continue level
5.
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?
hard
A.
for ($i = 1; $i <= 3; $i++) {
if ($i == 2) continue;
for ($j = 1; $j <= 3; $j++) {
if ($j == 2) continue;
echo "($i,$j) ";
}
}
B.
for ($i = 1; $i <= 3; $i++) {
for ($j = 1; $j <= 3; $j++) {
if ($j == 2) continue;
if ($i == 2) continue 2;
echo "($i,$j) ";
}
}
C.
for ($i = 1; $i <= 3; $i++) {
for ($j = 1; $j <= 3; $j++) {
if ($i == 2) continue 2;
if ($j == 2) continue;
echo "($i,$j) ";
}
}