TactivergeLite

Lite / Building Machines That Respond / Week 1

Flowcharts: drawing decisions and loops

A tank controller checks 'is the level equal to 90 L?' after each pump pass. Why might the tank overflow?

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

Why should I care?

Flowcharts are how engineers sketch the logic of a controller before it becomes code. A five-minute trace on paper can reveal a loop that never ends, which on a real pump means a flooded floor.

Predict

The tank starts at 20 L and each pump pass adds 15 L. The flowchart loops while level != 90. What happens?

  1. It stops at exactly 90 L
  2. It stops at 95 L
  3. It never stops: the level goes 80, 95, 110… and is never exactly 90
Guess first, then reveal

20, 35, 50, 65, 80, 95, 110 … the level jumps straight past 90 and is never equal to it, so 'level != 90' stays true for ever. With 'level < 90' the loop stops at 95 L.

Discover

Start at 20 L with the decision 'level < 90'. Predict how many times the pump runs, then press Step and fill in the trace table. Now switch to 'level != 90' and run it again. Try a start of 30 L too: why does != work there but not from 20 L?

What the interactive shows (text version)

The flowchart runner shows: Start (read level) → decision 'level < 90?' → if yes, pump one pass adding 15 L and return to the decision; if no, pump off and stop. A slider sets the starting level and a switch changes the decision to 'level != 90'. From 20 L the trace table reads 20, 35, 50, 65, 80, 95 (stop) with '<', but with '!=' the levels pass 90 and keep rising past the 120 L tank capacity.

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

Watch (optional)

Watch for: Watch how a sorting algorithm repeats the same few steps with a decision each time: the same loop-and-decide shape as the tank flowchart.

Shows how a step-by-step recipe of decisions and repeats lets a machine solve a problem such as sorting.

“Intro to Algorithms: Crash Course Computer Science #13” by CrashCourse (opens YouTube; streaming video uses a lot of data)

The video is optional. Everything you need is in this lesson's text.

Understand

Three shapes are enough

Rounded boxes mark start and end. Rectangles are actions (pump on for one pass). Diamonds are decisions with exactly two exits, yes and no. Arrows say what happens next. An arrow that leads back up to an earlier decision makes a loop.

Trace tables catch bugs on paper

A trace table has one row each time you reach the decision: the value of every variable and whether the test is true. Tracing 20, 35, 50, 65, 80, 95 by hand shows exactly when the loop stops, before any pump is wired up.

Stop on a range, not an exact value

Real quantities rarely land exactly on a target. Steps of 15 L skip over 90 L; a sensor reading of 89.97 is not 90. A stopping test such as level < 90 (keep going while below) always ends; level != 90 can run for ever. Engineers also add a safety limit, such as a maximum number of passes or a high-level float switch.

Explain it another way

Intuitive

Imagine filling a bath with a jug that holds 15 L and stopping only when the water is at exactly the 90 L line. You would pour past it and keep going, because 'exactly' never happens.

Technical

A flowchart is a directed graph of process and decision nodes. A loop is a cycle in that graph; it terminates only if the condition on its exit edge must eventually become true. Inequality tests guarantee that for a level that only increases; equality tests do not.

Mathematical

Passes n = ⌈(T − S)/A⌉ for S < T, otherwise 0. Final level = S + n·A, which lies in the range [T, T + A).

Applied

Industrial tanks combine a level sensor with an independent high-level switch that cuts the pump regardless of the controller. Overflow protection is a requirement, not an optional extra.

Derivation: How many passes does the loop need?
  1. The loop runs while level < target. Start level S, each pass adds A litres.

  2. After n passes the level is S + n·A. The loop stops at the first n where S + n·A ≥ target.

  3. Rearrange: n·A ≥ target − S, so n ≥ (target − S) / A.

  4. n must be a whole number of passes, so n is (target − S) / A rounded up. From 20 L with A = 15 L and target 90 L: (90 − 20) / 15 = 4.67 → 5 passes, finishing at 20 + 5 × 15 = 95 L.

  5. If S is already ≥ target, the condition is false at the first check and the pump runs 0 times.

Worked example 1

Start 40 L, add 25 L per pass, loop while level < 120. Trace it.

40 (True) → 65 (True) → 90 (True) → 115 (True) → 140 (False, stop). The pump runs 4 times and the tank ends at 140 L. Check: (120 − 40) / 25 = 3.2 → rounded up = 4.

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.

passes

Practice 2. In a flowchart, what does a diamond mean?
Practice 3. A loop runs while level != 100 and adds 30 L per pass, starting at 0 L. What happens?

passes

Practice 5. Why add 'or 20 passes have run' to the tank loop?

Apply it

Draw a flowchart for a phone charger that charges in 10 % steps while the battery is below 80 %, then stops. Trace it from 35 %. Then redraw it with an extra decision that stops charging if the battery is hotter than 45 °C.

Reflect

Where else could an exact-match test cause a machine to keep going for ever?

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

← Break a machine's job into stepsTrace every case with a rule table →