Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How can I stop a loop early or skip one iteration?

Quick answer

Use break to exit a loop early; execution continues after the loop. Use continue to skip the rest of the current iteration and move on to the next one. In a for loop, continue still allows the update expression to run before the next condition check. [S1] [S2]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I have a loop that should stop when it finds a particular value. Should I use break?

Geez

Yes. An unlabeled break exits the innermost loop immediately, and the program continues with the statement after that loop. [S1] Here is a small JavaScript example:

for (let i = 0; i < 6; i++) {
  if (i === 3) {
    break;
  }
  console.log(i);
}
console.log("Done");

Exact output:

0
1
2
Done

When i becomes 3, break exits before console.log(i) runs for that value. The loop does not print 3, 4, or 5; it moves straight to the console.log("Done") after the loop. The example counts upward from zero and stops when i === 3.

Follow-up question

What if I want to skip just one value, then keep checking the rest?

Geez

Use continue when you want to skip the rest of the current iteration without ending the loop. In a for loop, control proceeds to the update expression and then checks the loop condition again. [S2]

for (let i = 0; i < 6; i++) {
  if (i === 3) {
    continue;
  }
  console.log(i);
}

Exact output:

0
1
2
4
5

At i === 3, continue skips console.log(i) for that pass. The loop then updates i and carries on, so 4 and 5 are still printed. Change the condition to match the value or rule you want to skip.

Follow-up question

How do I decide between them, especially if the loop contains other code?

Geez

Ask whether you want to stop the loop entirely or only leave out work for one pass:

  1. Choose break if the loop has no reason to continue. Statements after the loop can then run.
  2. Choose continue if later iterations still matter. Put it where skipping the remaining statements for this pass is intended.

For example, if a loop checks values and should stop at the first match, break expresses that stopping point. If it should process every value except one, continue expresses the exception. Check what happens to statements below the control statement: with continue, they are skipped for that iteration; with break, the rest of the loop is exited. These examples describe JavaScript; check the relevant language documentation when applying the pattern elsewhere.

Follow-up question

Can you give me a reusable way to check that I used the right one?

Geez

Try this quick review with your own loop:

  1. Identify the exact condition that triggers the change in control flow.
  2. If that condition means “no more iterations are needed,” use break.
  3. If it means “skip this pass, but process later values,” use continue.
  4. Trace the next step: after break, execution leaves the loop; after continue in a for loop, the update runs before the next condition check. [S1] [S2]
  5. Trace a small range and write down the expected output. For the examples above, the break version prints 0, 1, 2, while the continue version prints 0, 1, 2, 4, 5. If your output differs, check the condition and whether the print or processing statement appears before or after the control statement.

A practical checklist

  • Decide whether the loop should end or only skip its current pass.

  • Check that the condition matches the value or situation you intend.

  • Confirm which statements are skipped when control reaches break or continue.

  • For a for loop using continue, account for the update before the next condition check.

  • Trace a few iterations and compare them with the expected output.

A few more useful answers

Does `continue` stop the loop?

No. It skips the rest of the current iteration and proceeds toward the next one; it does not terminate the loop. [S2]

Does `break` exit every loop when loops are nested?

An unlabeled break exits the innermost loop it is in, not all enclosing loops. [S1]

Sources

References used for this guide.

  1. [S1] break — JavaScript | MDN
    developer.mozilla.org · Checked
  2. [S2] continue — JavaScript | MDN
    developer.mozilla.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work