Course contentsShow
← JavaScript
Lesson 365 of 2,72310. LoopsFree lesson

Continue in nested loops: Affects innermost iteration

See how continue skips to the next iteration of the innermost loop, leaving outer loops unaffected.

Continue in Nested Loops: Affects Innermost Iteration

You'll learn how the continue statement only skips to the next iteration of the innermost loop where it appears, while outer loops keep running normally.

The Core Idea

When you use continue inside nested loops, it only affects the loop it's directly inside — the innermost one. The outer loop continues its normal execution as if nothing happened. Think of nested loops like rooms within rooms: continue only kicks you out of the innermost room, but you're still inside all the outer rooms.

How It Works

for (let i = 1; i <= 3; i++) {
  console.log(`Outer loop: ${i}`);
  
  for (let j = 1; j <= 3; j++) {
    if (j === 2) {
      continue; // Skips only inner loop's iteration when j is 2
    }
    console.log(`  Inner loop: ${j}`);
  }
}

Output:

Outer loop: 1
  Inner loop: 1
  Inner loop: 3
Outer loop: 2
  Inner loop: 1
  Inner loop: 3
Outer loop: 3
  Inner loop: 1
  Inner loop: 3

Notice how when j equals 2, we skip that inner iteration but the outer loop with i keeps going through all its values (1, 2, 3). The continue doesn't stop or affect the outer loop at all — it just jumps to the next value of j.

Important Distinction

If you wanted to affect an outer loop, you'd need labeled statements (which you learned earlier). But plain continue always targets the loop it's immediately within.

Key Takeaway: The continue statement only skips to the next iteration of the innermost loop containing it; outer loops continue their normal execution unaffected.