Beep! Tap the happy computer!
Decisions & loops

What if the computer never gets bored?

# Loops that repeat Computers do not get bored. **Loops** repeat work. ## for loop ```python for i in range(5): print("Hello", i) ``` `range(5)` gives 0,1,2,3,4 — five times. ## while loop ```python n = 3 while n > 0: print(n) n = n - 1 ``` ## Danger A `while True:` with no `break` can run **forever**. That is an infinite loop. ## Why loops exist Printing the 1–10 table by hand is tiring. A loop does it in four lines — and never forgets 7 × 8. > If your program never stops, you probably forgot to change the loop variable. ## Loops are algebra’s sequences wearing running shoes The sequence 3, 6, 9, 12 is `3n` for n = 1, 2, 3, 4. In code: ``` for n in range(1, 5): print(3 * n) ``` Same table, automatic. ### Two loop families - **Counted** — you know how many times (for / repeat 10) - **Conditioned** — you know when to stop (while lives > 0) Both must be finite. Counted loops usually are. Conditioned loops need a change that will end the condition. ### When you are stuck Write the body once in English. Then say “repeat this for each …”. Then only wrap the loop.

By the end of this lesson

Predict how many times a repeat-n loop runs, and say why loops beat copy-paste.

Remember

  • A loop repeats a block. Computers do not get bored.
  • repeat 5 / for i in range(5) runs the body five times.
  • A while loop keeps going as long as a condition is true — it must eventually become false.
  • Loops match “do this for every student” and “print the 5 times table”.
  • Copy-pasting the same print 20 times is how bugs multiply. A loop is one idea, many runs.

Worked examples

Cover the answer. Try the steps on paper, then open them.

Example 1 — Count prints

for i in range(3): print("Hi")

  1. range(3) gives three turns
  2. Hi printed three times, each on its own line

Answer: Hi × 3

Check: i is the seat number 0,1,2 unless you use it.

Example 2 — Table

Print the first four multiples of 5 with a loop.

  1. n goes 1,2,3,4
  2. each time print 5*n
  3. 5, 10, 15, 20

Answer: 5 10 15 20

Check: This is the nth-term idea from algebra: 5n.

Example 3 — Danger while

while True: print("Hi") with no break.

  1. Condition is always true
  2. It never stops — infinite loop
  3. You must stop the program by hand

Answer: must have a way out

Check: Finite-ness from the algorithm lesson.

Common mistakes

Slip Thinking range(5) runs when i is 5 (five included) in beginner Python.

Fix range(5) is 0,1,2,3,4 — five numbers, last is 4.

Count how many, or use range(1, 6) for 1…5.

Slip Copy-paste print 10 times, then needing to change the message in 10 places.

Fix One print inside a loop, change once.

That is the point.

Slip A while with no update to the variable in the condition.

Fix If n starts at 1 and you never change n, n < 5 stays true forever.

Update inside the loop.

Try on paper first

Write the answer, then tap Reveal. These are not the quiz — they are warm-ups.

1. How many times does repeat 7 run the body?

Answer: 7

2. Why use a loop instead of 20 pasted lines?

Answer: One change updates all runs; less bugs

3. A loop that never ends is called…

Answer: Infinite loop

4. Printing 3,6,9,12 with 3n is a loop plus what algebra idea?

Answer: nth term / substitution

Touch practice

8 tap / type

Tap or type. Use the hint if you need it. Retry any miss — the why is the lesson.

Optional picture lab

After practice

Real world

Clapping 5 times.

Connect

Sequences and tables in maths.

⭐ Star quiz

9 questions

Do the worked examples and touch practice first. Then take the quiz without peeking.