Portmint Lighthouse

Loops, Doing It Again

Here is the plain idea: a loop repeats a set of steps. Instead of writing the same instructions over and over, you write them once and tell the computer, "do this again, and again, until I say stop."

Computers are tireless and fast. Repetition is exactly what they're best at, and loops are how you put that to work.

The laps-around-the-track analogy

Picture a runner doing laps. The instruction isn't "run lap one, run lap two, run lap three, run lap four." It's simpler: "run around the track until you've done four laps." One instruction, repeated, with a clear rule for when to stop.

A loop is that instruction. You describe the steps once — the lap — and give a stopping rule: a count to reach, or a condition to watch for. The computer runs the lap over and over until the rule says it's done.

Two everyday flavors

Most loops come in one of two shapes.

A count loop repeats a set number of times. "Do this 10 times." "Print every name in the list." You know up front how many laps to run.

A condition loop repeats while something stays true. "Keep stirring while the sauce is too thin." "Keep asking for a password until it's correct." You don't know how many laps it'll take — you just keep going until the condition flips.

Notice the condition loop leans on the same yes-or-no question from the conditionals lesson. Before each lap, the computer checks: still true? Then go around again. False? Stop and move on.

The runaway loop

Here's the one danger to respect. If the stopping rule never becomes true, the loop runs forever — the program spins in place, doing the same thing endlessly, never reaching the next instruction. Programmers call this an infinite loop, and everyone writes one by accident eventually.

The fix is to make sure each lap moves you toward the finish. If your rule is "stop when the count reaches 10," then something inside the loop had better add to the count each time. A runner who never counts their laps runs until they collapse.

Why loops are everywhere

Almost everything a program does in bulk is a loop. Send an email to every customer. Add up every number in a receipt. Check every row in a spreadsheet. Show every photo in an album. The work differs, but the shape is identical: take a set of steps, and repeat them across many things.

So: a loop is running laps, you describe the lap once, and a stopping rule decides when to quit — just don't forget to move toward the finish.

Your turn

No computer needed. For each chore below, decide whether it's a count loop (you know the number of laps up front) or a condition loop (you stop when a rule flips):

  1. Doing twenty push-ups.
  2. Stirring soup until it's warm.
  3. Reading every page of a 12-page chapter.
  4. Refreshing a webpage until the photo finally loads.

Then spot the runaway: "Keep walking forward until you reach the wall" — in a room with no wall. Why does that loop never stop, and what one change would fix it?

(Answers: 1 and 3 are count loops; 2 and 4 are condition loops. The runaway never ends because its rule can't come true — add a wall, or swap in a real limit like "until you've taken ten steps.")

Next, we'll bundle steps under a single name. 🐙

Stuck or curious?

Ask Pip about this lesson — tap the porthole bottom-right.