🔁 Loops: Do It Again

3rd Grade 💻 Coding

Say you want the character in your game to jump a hundred times. You could write “jump” a hundred times — or you could write once, “repeat 100 times: jump.” That’s a loop, one of the most powerful tools in programming. In this lesson we learn about different kinds of loops, and how not to get stuck in a loop that never ends.

Free · US Letter · no sign-up
Google Classroom Save to Pinterest Email me

Why we need loops

Computers are great at something people hate: doing the same thing over and over without getting bored and without making mistakes. A loop is an instruction that says “repeat these instructions.” Instead of writing ten identical lines, you write them once and wrap them in a loop. The code is shorter, easier to read and easy to change: if you want 20 repeats instead of 10, you change one number.

Loops are everywhere in life: “stir ten times,” “run around the field three times,” “sing the chorus again.” A traffic light is a loop too — green, yellow, red, and back to the start, all day long. In programming, a loop lets you draw a hundred stars, check every student on a list, or keep moving a character again and again for as long as the game is running.

A loop with a set number of repeats

The simplest loop repeats a fixed number of times. In Scratch there’s a “repeat 10” block, and inside it you put the blocks that should repeat. If you put “move 10 steps” and “turn 36 degrees” inside it, the sprite draws an almost-perfect ten-sided circle — because 10 times 36 degrees is a full turn of 360 degrees.

In Python you use for. The code for i in range(3): print(i) shows the numbers 0, 1 and 2 — three repeats. The variable i is called a counter: it keeps track of which repeat we’re on. Notice that in Python counting starts at 0, and the number of repeats is the number in the parentheses. If you want to count from 1 to 3, you write range(1, 4) — the last number isn’t included.

The “while” loop

Sometimes you don’t know ahead of time how many times to repeat. “Keep playing while you still have lives.” “Keep asking until the answer is right.” For that there’s a different kind of loop: while in Python, and “repeat until…” in Scratch. The loop checks a condition before every repeat. If the condition is still true, it goes around again; if not, it stops and the program moves on.

For example: count = 3 followed by while count > 0: count = count - 1. Each time around, count goes down by one: 3, 2, 1, 0. When it reaches 0, the condition “count is greater than 0” isn’t true anymore, and the loop stops. That’s how loops and conditions work together — and it’s one of the most important ideas in all of programming.

The danger: a loop that never ends

What happens if we forget the line that makes count smaller? The condition stays true forever, and the loop keeps going and going. That’s called an infinite loop. The program “freezes” — it stops responding, or shows the same thing over and over — until someone forces it to stop. When this happens in Python, you can stop the program by pressing Ctrl+C, and in Scratch by clicking the red stop sign.

Still, sometimes you want an infinite loop on purpose. A game has to keep checking whether a key was pressed, and a clock has to keep ticking without stopping. Scratch has a “forever” block for this. The difference is that it’s planned: the game stops when you close it. The rule for programmers: in every “while” loop, make sure something inside it changes, so that one day the condition stops being true.

Key facts

  • A loop is an instruction to repeat a group of instructions several times.
  • In Python, for fits a known number of repeats, and while repeats as long as a condition is true.
  • In Python, range(3) gives the numbers 0, 1 and 2 — counting starts at 0.
  • An infinite loop never ends, because its condition is always true.
  • Scratch has “repeat 10,” “repeat until…” and “forever” blocks.

Quick quiz

  1. What does a loop do?

  2. What does the code for i in range(3): print(i) show?

  3. Which loop fits “keep going while you still have lives”?

  4. What is an infinite loop?

  5. A sprite moves and turns 36 degrees, 10 times. What does it draw?

  6. What is the job of a counter (like i) in a loop?

  7. Which Scratch block do you use to repeat with no end?

  8. How does a loop help when you want 20 repeats instead of 10?

💭 Think and talk about it

  1. Find three loops in your life. For which of them do you know the number of repeats ahead of time?
  2. How would you draw a triangle with a loop? How many degrees do you need to turn at each corner?
  3. Why do you think a computer game needs a “forever” loop?

Questions kids ask

What’s the difference between for and while?

for fits when you know how many times to repeat, or when you go through a list. while repeats as long as a certain condition is true, and fits when you don’t know ahead of time how many repeats there will be.

How do you stop an infinite loop?

In Python, press Ctrl+C in the window where the program is running. In Scratch, click the red stop button. Then fix the code so the condition eventually stops being true.

Why does counting start at 0 in Python?

That’s how most programming languages do it, mainly because of the way positions in a list are numbered: the first item is at position 0. It’s confusing at first, and then you get used to it.

🔎 Ask Thinkapedia another question