sciandu
Computer science

Computer science

Loops with conditions

Repeat until it fits: loops that do not count, they check.

What you need first

When you stir a batter, you do not count to 50 and then stop. You stir as long as the batter is still lumpy, and you stop as soon as it is smooth. Loops with conditions work exactly like that: they do not repeat something a fixed number of times, they repeat as long as a condition holds.

Fixed count or condition?

So far you knew loops with a fixed number of passes: do this 10 times. But sometimes you do not know the number in advance. How often do you have to roll a die until you get a six? How often do you have to pump until the tire is full? That is what the loop with a condition is for: repeat as long as the condition is true. Before every pass the condition is checked. If it is false, the loop is over. In programming languages it is usually called a while loop, because while means exactly that: keep going as long as the condition holds.

Decision switch
Temperature12 °C
if t ≤ 0: scarf and hat
else if t < 20: jacket
else: T-shirt
At 12 °C: jacket
Try it: change the condition and see when the program keeps going and when it stops.

The exit condition

For the loop to end at some point, something has to change in every pass that sooner or later makes the condition false. Example: as long as the bucket holds less than 20 liters, pour in 3 more. In every pass the amount of water rises, so the condition eventually becomes false and the loop stops. That change is the most important part of the whole loop. But watch where the loop stops: a pass that has started always runs through completely. If the condition still holds before you pour, the full 3 liters go in, even if that pushes the bucket past 20 liters. So a condition loop stops at the first value that no longer meets the condition, and here that value is already past 20 liters.

Infinite loops: the classic bug

What happens if the condition never becomes false? The loop runs forever. That is called an infinite loop, and it is one of the most famous programming bugs of all: the program hangs, the screen freezes, the fan spins up. Usually the cause is tiny: someone forgot to change the counter, or the condition checks the wrong thing. For every condition loop, ask yourself: what exactly makes it stop eventually?

Exercises

0 of 6 solved

Time to try it yourself. You can't break anything, every attempt counts.

You do not know in advance how often something must be repeated. Which loop fits?

The condition of a loop never becomes false. What happens?

A number starts at 10. As long as it is greater than 0, 2 is subtracted. How many passes does the loop make?

How does a loop with a condition run until it stops? Put the steps in the correct order.

  1. 1Because it is true, the pass runs
  2. 2A value changes, for example the counter goes up
  3. 3Before the pass, the condition is checked
  4. 4At the next check the condition is false and the loop stops

A number starts at 0. As long as it is less than 8, 1 is added. How many passes does the loop make?

If the condition of a loop never becomes false, you get .

Where this leads