Definition
A washing machine fills with water, then keeps checking: is it full yet? Not yet… not yet… now! How do you draw "keep checking until" on a flowchart?
A decision lets an algorithm take different paths depending on a condition. A loop sends the flow back to an earlier step so that some steps repeat, until a condition says to stop. In a flowchart, a loop is drawn as an arrow from a decision back up to an earlier box.
In simple words
When you climb stairs, you do not write "step, step, step" twenty times. You think: "take a step; am I at the top? If not, take another step." That one question, asked again and again, is a loop.
Why it matters
Real tasks are full of choices and repetition. Decisions and loops let a short algorithm handle many situations and huge amounts of work. A machine learning system is trained by a loop that repeats thousands of times, improving a little on each pass.
Key terms
- Condition
- A question whose answer is yes or no, true or false.
- Branch
- One of the paths an algorithm can take after a decision.
- Loop
- A set of steps that repeats.
- Stopping condition
- The condition that ends a loop, such as "the tank is full".
- Counter
- A number that keeps track of how many times a loop has run.
- Infinite loop
- A loop that never stops because its stopping condition is never met.
Explained in detail
Decisions: choosing a path
A decision splits the flow into two branches. "Is it raining?" Yes: take an umbrella. No: go straight out. Decisions can also be chained: "Is the mark 90 or more?" Yes: grade A. No: "Is it 75 or more?" Yes: grade B. No: "Is it 50 or more?" and so on. Each diamond asks one clear question. The order matters: if you asked "50 or more?" first, a mark of 95 would wrongly stop there.
Loops: repeating steps
There are two common kinds of loop. A counting loop repeats a fixed number of times: "Do 10 push-ups" means count, do one, add one to the counter, and check whether the counter has reached 10. A condition loop repeats until something becomes true: "Stir until the sugar dissolves" might take five stirs or fifty. In a flowchart, both look the same: a decision diamond whose "not yet" arrow loops back up to an earlier step, and whose "done" arrow carries on down.
Tracing a loop
| Pass | Water in the tank | Full? | Next step |
|---|---|---|---|
| 1 | 20 litres | No | Add 20 litres, check again |
| 2 | 40 litres | No | Add 20 litres, check again |
| 3 | 60 litres | Yes (tank holds 60) | Stop filling, start washing |
Writing out each pass like this is called tracing. It is the best way to check that a loop does what you think, and that it stops.
The danger of infinite loops
If the stopping condition can never be met, the loop runs forever. Suppose the tank holds 50 litres but the machine adds 20 at a time and checks "exactly 50?". It goes 20, 40, 60, and never equals 50, so it keeps filling and overflows. The fix is a safer condition, "50 or more?", and often a safety limit such as "stop after 10 passes whatever happens". Real machines and programs include such limits because a stuck loop can waste energy, freeze a phone or flood a floor.
Loops in AI
Training a machine learning model is a loop: make predictions on the examples, measure the mistakes, adjust the model a little, and repeat, until the mistakes stop getting smaller or a set number of passes is reached. Each pass through all the training data is called an epoch. A chatbot replying to you also uses a loop: it picks the next word, adds it to the reply, and repeats until it decides the reply is finished.
How it works
- Find the steps that repeat.
- Write the stopping condition as a yes-or-no question.
- Draw the repeating steps, then the decision diamond.
- Draw the "not yet" arrow back up and the "done" arrow onwards.
- Trace a few passes and make sure the loop always stops.
Try it yourself
The tank that overflowed
A team designs the flowchart for a water pump that fills a 50-litre tank.
Decision or loop?
Is each instruction mainly a decision or a loop?
Water each plant in the row
If it is Sunday, sleep late
Knead the dough until it is smooth
If the light is red, stop
Keep ringing the bell until someone answers
Common mistakes
- Writing a loop whose stopping condition can never be met.
- Putting chained decisions in the wrong order.
- Forgetting to update the counter inside a counting loop.
Check yourself
Summary
- Decisions choose between branches; loops repeat steps.
- Every loop needs a stopping condition that will certainly be met.
- Trace loops pass by pass; AI training itself is a loop.
Finished reading?
Sources: BBC Bitesize: Introduction to computational thinking · Elements of AI (University of Helsinki)