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.
Stop correctly
Use break when later iterations are unnecessary.
Skip correctly
Use continue without skipping a necessary state update.
Separate control actions
Explain how pass and return differ from loop control.
Skip a missing mark without losing later marks
Recorded mark
Add the first valid mark.
total → 40Missing mark
Continue skips addition for null.
total stays 40Later mark
The next iteration still runs.
total → 75
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
75Stop when the first student is found
The loop prints the names it inspects. Once the target matches, it prints Found and break leaves the loop; Asha is never inspected.
A successful break does not mean every record was processed. Use it only when later records are irrelevant to the task, such as a first-match search.
JavaScript · local teaching example
const names = ['Ravi', 'Maya', 'Asha'];
for (const name of names) {
console.log(name);
if (name === 'Maya') {
console.log('Found');
break;
}
}Expected output
Ravi
Maya
FoundApply 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
RaviRecognize 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);Apply the idea to a new situation
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.