TactivergeLite

Lite / Building Machines That Respond / Week 6

while loops: repeat until it's done

A motor cools by 20 % of its excess heat each minute. How long until it is safe to touch, without knowing the answer in advance?

About 35 min · 3 goals · Checked by automated tests · Full interactive version

Why should I care?

Sometimes you don't know how many steps a job takes: cool until safe, fill until full, retry until a reply arrives. A while loop keeps going until a condition stops being true, which is exactly how many simulations and controllers work.

Predict

excess = 60.0, and while excess > 5: excess = excess * 0.8. Roughly how many passes?

  1. About 3
  2. About 12
  3. It never stops
Guess first, then reveal

12 passes: 60 → 48 → 38.4 → … → 5.15 → 4.12. Each pass removes 20 % of what is left, so it slows down but always gets below 5 eventually.

Discover

In the Code Lab, type the cooling program from this lesson and run it: do you get 12 minutes? Then change 0.8 to 0.9 (slower cooling) and predict before running. Finally, break it on purpose by deleting the multiply line, and add a safety limit so it still stops.

What the interactive shows (text version)

In the Code Lab the cooling program prints '12 4.12': the excess temperature falls 60.0, 48.0, 38.4, 30.72, 24.58, 19.66, 15.73, 12.58, 10.07, 8.05, 6.44, 5.15, 4.12 °C, so the loop stops after 12 passes. With 0.9 instead of 0.8 it takes 24 passes.

You can also open the interactive on the full site when you have a faster connection.

Understand

The cooling simulation

excess_c = 60.0      # motor is 60 °C above the room
minutes = 0
while excess_c > 5:
    excess_c = excess_c * 0.8
    minutes += 1
print(minutes, round(excess_c, 2))   # 12 4.12

The condition is checked before every pass. If it is false at the start, the body never runs.

Every while loop needs progress

Something inside the loop must move it towards stopping: here excess_c shrinks. If nothing changes, the loop runs for ever. For machines, add a guard as well: while excess_c > 5 and minutes < 120: stops even if the model or a sensor misbehaves.

for or while?

Use for when you know what to go through: every reading in a list, each of 10 test cases. Use while when you stop on a condition: until the tank is full, until the error is small, until the user types 'q'. A for loop over a list always ends; a while loop ends only if you make it.

Explain it another way

Intuitive

Stirring tea until the sugar has dissolved: you don't count stirs in advance, you check each time and stop when it's done.

Technical

while evaluates its condition before each iteration and exits on False. Termination needs a variant: a quantity that changes monotonically towards the exit condition, plus a hard iteration limit in safety-relevant code.

Mathematical

Excess after n minutes: E·kⁿ (here 60 × 0.8ⁿ). The loop finds the smallest n with 60 × 0.8ⁿ ≤ 5, i.e. 0.8ⁿ ≤ 1/12. Since 0.8¹¹ ≈ 0.086 > 0.083 and 0.8¹² ≈ 0.069, n = 12. This is a simplified cooling model with a fixed fraction lost per minute.

Applied

Simulations step time forward in a while loop until something happens: a tank fills, a robot reaches its goal, a temperature settles. The same pattern appears in retry loops for radio messages.

Worked example 1

A tank holds 1000 L and drains 15 % of what is left each minute. With a while loop, how many minutes until it is below 500 L?

1000 → 850 → 722.5 → 614.1 → 522.0 → 443.7. After 5 minutes it is below 500 L, so the loop runs 5 times: while level_l >= 500: level_l = level_l * 0.85; minutes += 1.

Common mix-ups

Practice

Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.

min

Practice 2. Which while loop never ends?
Practice 3. Which job suits a while loop best?
Practice 4. x = 2. How many times does while x > 5: x = x - 1 run?

Apply it

Simulate a phone charging: start at 12 % and add 7 % each 10-minute step while below 80 %. Print each step and the total time. Then add a guard so the loop also stops if the battery temperature (add 0.4 °C per step from 25 °C) exceeds 35 °C.

Reflect

What is the safety limit you would add to any while loop that controls real hardware, and why?

Write your answer on paper or in your own notes. Lite never stores what you write.

← Count, flag and find extremesPackage a calculation as a function →