Lesson 13 of 16 · 50 min · UG / professional
pass, early stop, and else on loops
pass is a legal empty body. break/continue are laptop spellings of ‘stop now’ and ‘skip this turn’. A loop’s else runs when you never stopped early — useful in a search that must report ‘not found’.
Roll-number search
Scan a list; sort a copy.
You will be able to
- ✓ Use pass as a legal empty body
- ✓ Model break/continue with a flag in this sandbox
- ✓ Know loop-else means ‘never stopped early’
pass, break, continue
pass does nothing — a placeholder for a function you will fill, or an empty class on a laptop. break leaves the innermost loop now. continue skips the rest of this turn. This teaching sandbox does not run break/continue; a flag (found = 1) is the same policy, visible in the code.
An else attached to for/while on a laptop runs when the loop finished normally — not after break. That is how a prime-search example reports ‘no factor found’. Here: if found == 0: print("Missing") after the loop.
Search vs filter
A search stops after the first hit. A filter keeps collecting. Do not mix them. continue is ‘this row is uninteresting’; break is ‘we are done’.
Laptop: break, continue, loop else
for n in range(2, 6):
if n % 2 == 0:
print("even", n)
continue
print("odd", n)
else:
print("finished without break")
Words that matter
- pass
- A legal empty body.
- break
- Leave the loop now (laptop).
- loop else
- Runs if the loop was not broken (laptop).
Common mistakes
Avoid: Using pass as the only error handling.
Do this: pass is a placeholder, not a policy.
Run it step by step
Each box is a real program. Press Run, change a number, Run again — the output must follow your code.
1. Step 1 — pass as a stub
The function exists. Later you will fill it. Today it returns 0.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
2. Step 2 — first match, then ‘not found’
Flag stands in for break. Zero means loop-else energy.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
Example program — Stop after the first heat
34 is enough. The rest can wait.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
Your turn — Print Missing
codes = ["A", "B"]. Search for "Z". Print Missing when it is not there.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
Self-assessment
Check your understanding before you mark the lesson complete.
Progress is stored in a browser cookie on this device.