Break, Continue and Pass: Control Repetition

Stop after a match or skip an unsuitable mark

A teacher searches a class list for Maya and can stop when she is found. A different report should skip missing marks but continue adding the recorded ones. These require different loop actions.

break leaves the nearest enclosing loop. continue skips the remaining body of the current iteration and proceeds according to the loop’s next-iteration rules. Python pass is a placeholder that does nothing.

  1. Stop correctly

    Use break when later iterations are unnecessary.

  2. Skip correctly

    Use continue without skipping a necessary state update.

  3. Separate control actions

    Explain how pass and return differ from loop control.

Skip a missing mark without losing later marks

  1. Recorded mark

    Add the first valid mark.

    total → 40
  2. Missing mark

    Continue skips addition for null.

    total stays 40
  3. Later mark

    The next iteration still runs.

    total → 75
Read these stages alongside the complete example. They explain the relationship; they do not execute code.

Leaving a loop differs from leaving an iteration

Use break after finding an item when the first match is enough. Use continue to ignore an item while preserving later work. Neither instruction validates data by itself; you decide which case should trigger it.

A return leaves the current function, possibly ending a loop inside it as a consequence. Python pass simply satisfies a syntactic requirement for a statement; it neither skips an iteration nor stops a loop. JavaScript has no pass keyword.

Before starting, review iterations and changing loop state. These examples use JavaScript; other languages can use different syntax and rules.

Skip missing marks and keep adding the rest

The null check uses strict equality. For null, continue bypasses total += mark. The for...of loop then supplies the next value, 35.

The output is 75. This is a deliberate missing-data policy: the example does not silently claim a complete three-subject total. A real report should say which results were omitted.

JavaScript · local teaching example

const marks = [40, null, 35];
let total = 0;
for (const mark of marks) {
  if (mark === null) continue;
  total += mark;
}
console.log(total);

Expected output

75

Apply the rule to a small change

Search for Ravi at the first position and print only the matched name. Explain why the later names are not read.

Worked answer

The first item matches and break ends the loop. If the task were to find every duplicate name, breaking after the first would be the wrong rule.

JavaScript · local teaching example

const names = ['Ravi', 'Maya', 'Ravi'];
for (const name of names) {
  if (name === 'Ravi') {
    console.log(name);
    break;
  }
}

Expected output

Ravi

Recognize mistakes and limits

In nested loops, an ordinary break or continue targets the innermost loop. Do not assume it leaves all repetition. JavaScript labeled control exists, but restructuring into clear functions is often easier to read.

A continue in a while loop can bypass a counter update placed later in the body and prevent termination. Move essential progress before the skip or restructure the loop. Do not substitute Python pass for continue: after pass, later statements still run.

For the language or tool rules used here, see JavaScript iteration rules.

Check the core idea

JavaScript · code for this question

const marks = [40, null, 35];
let total = 0;
for (const mark of marks) {
  if (mark === null) continue;
  total += mark;
}
console.log(total);
What does continue do when the null mark is reached?
Choose one answer. You can try again.

Answer and explanation

It skips this iteration’s remaining body and lets later iterations proceed.

Yes. The later 35 is still added.

Apply the idea to a new situation

What is the role of Python pass?
Choose one answer. You can try again.

Answer and explanation

It performs no operation while providing a statement where syntax requires one.

Yes. It is a placeholder, not a loop skip.

Name what should stop before selecting the instruction

Break leaves a loop, continue skips its current remaining body, and return leaves a function. Python pass does nothing. Preserve necessary updates and distinguish first-match searches from complete processing.

Connect this idea with progress and termination and function return behavior.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Infinite Loops and Reliable Exit Conditions →