1.2.20 · D1Introduction to Programming (Python)

Foundations — break, continue, pass — when and why

2,786 words13 min readBack to topic

Why this page exists

The parent note (break, continue, pass — when and why) throws around words like iteration, body, block, condition, range, %, enumerate, and control flow as if you already own them. Here we earn each one, in an order where every idea rests on the one before it.


1. A statement — the smallest unit of "do something"

Picture a single index card, each holding one order: "print hello", "add 1 to x". Python reads cards top to bottom, one at a time.

print("hi")      # one statement
x = x + 1        # another statement

Why the topic needs it: break, continue, and pass are themselves statements. pass in particular only makes sense once you know Python demands a statement in certain spots.


2. A block — statements bundled by indentation

Figure — break, continue, pass — when and why

Figure 1 (above): a for header ending in a colon, then three indented lines wrapped by a yellow bracket labelled "the block (body)", then a final line dedented back to the left showing the block has ended. The yellow bracket groups the indented lines; everything at the same indentation belongs to the same block. When indentation goes back to the left, the block has ended.

Why the topic needs it: the "body" the parent talks about is this block. break/continue skip parts of it; pass fills it.


3. A condition — a yes/no test

Examples of questions and their answers:

Expression Reads as Result
x == 2 "is x equal to 2?" True / False
n % 3 == 0 "does n divide evenly by 3?" True / False
i * i <= n "is i squared less than or equal to n?" True / False

Why the topic needs it: every break/continue in the parent lives inside an if <condition>: — the keyword only fires when the test is True. See Conditionals (if-elif-else).


4. The % operator — remainder after division

Picture sharing a sweets among b kids as evenly as possible; % is how many sweets are left in your hand.

The magic trick: n % k == 0 means "k divides n evenly". That is exactly how the parent detects multiples of 3 and how Prime Number Checking tests for divisors.


5. A loop — the repeating machine

Figure — break, continue, pass — when and why

Figure 2 (above): three rounded boxes joined by arrows forming a cycle — "take next item / check condition" (blue) at the top, an arrow down to "run the body" (yellow), a curved "repeat" arrow back up to the top, and a green exit arrow to "no items left → stop". A for loop's cycle is: take the next item → run the body → come back → take the next item... until items run out. A while loop's cycle is: check condition → run body → recheck → ... until the condition is False.

Why the topic needs it: break/continue/pass are loop-steering commands (well, pass is broader, but the parent uses it inside loops too). Without knowing what an iteration is, "skip this iteration" is meaningless.


6. range(...) — a made-to-order sequence of numbers

Why the topic needs it: almost every for example in the parent iterates over a range.


7. enumerate(...) — item AND its position

for i, n in enumerate(["Asha", "Ravi"]):
    print(i, n)   # 0 Asha  /  1 Ravi

Picture a coat-check: each coat (item n) comes with a numbered tag (index i).

Why the topic needs it: the parent's search example uses enumerate so that when break fires it can report where the match was found.


8. Control flow — the path execution takes

Figure — break, continue, pass — when and why

Figure 3 (above): a dotted vertical "loop top" line on the left, three "statement" lines stacked in the middle (the body), and an "AFTER loop" marker on the right. Three coloured arrows leave the body: a blue arrow curving back to the loop top ("normal: next iteration"), a yellow arrow jumping early back to the top ("continue: skip rest"), and a red arrow shooting out to "AFTER loop" ("break: leave loop").

  • normal (blue): fall straight through to the next line, then next iteration.
  • continue (yellow): jump early back to the top of the loop for the next item.
  • break (red): leave the loop completely, land on the first line after it.

9. The loop else — the "finished cleanly" branch

Picture a security guard checking every door: if they get through all doors without an alarm (break), the else says "all clear". If an alarm fired (a break), the "all clear" is skipped. Full detail lives in for...else and while...else.


Prerequisite map

Statement one instruction

Block indented body

Loop repeats a block

Condition True or False

if chooses to act

Modulo percent remainder

range number sequence

enumerate index plus item

Control flow the path taken

break continue pass

Loop else finished cleanly


Equipment checklist

Test yourself — each line is a question ::: answer you should be able to fill instantly.

What is a statement?
One complete instruction Python can execute; it may span multiple lines inside brackets or via a backslash.
How can one statement span several lines?
Automatically inside ()/[]/{}, or explicitly with a trailing backslash \.
What marks a block in Python?
Indentation under a line ending in a colon :.
Why can't a block be empty?
Python's syntax requires at least one statement; that's what pass provides.
What does a condition evaluate to?
True or False (a yes/no answer).
What does <= mean?
Less than or equal to.
Difference between = and ==?
= assigns a value; == compares two values and returns True/False.
What does 7 % 3 give and what does it mean?
1 — the remainder after dividing 7 by 3.
What does n % k == 0 tell you?
That k divides n evenly (no remainder).
What is one iteration?
One full run of the loop's body.
What numbers does range(4) produce?
0, 1, 2, 3 (stops before 4).
What does the third argument of range do?
Sets the step (stride); e.g. range(0,10,2) gives 0,2,4,6,8, and a negative step counts down.
Do range and enumerate return lists?
No — they return lazy iterables that yield one value at a time; use list(...) to materialize them.
What does enumerate give you each step?
Both the index (position) and the item.
How do you make enumerate start counting at 1?
enumerate(S, start=1).
What is control flow?
The order in which statements are actually executed.
In nested loops, which loop does break exit?
Only the innermost (nearest enclosing) loop.
How does while True: avoid running forever?
A break inside its body is the only way out.
When does a loop's else run?
Whenever the loop finishes without hitting break — including when it ran zero iterations.