Skip to content
ExamSpine

Lesson 3.9

Read and trace a loop flowchart

Part 3: RepetitionReading, about 6 minOptional2 exercises in this lesson

Reading a loop in a flowchart

By the end of this you will be able to find the loop in a flowchart, say which boxes repeat and which box is the update, trace the flowchart with a table, and say in a sentence what it does.

A flowchart is an algorithm drawn as boxes joined by arrows. A question may give you one instead of code. You already know everything a loop flowchart does from the lessons on while loops. This lesson is about reading the picture.

The shapes

Each kind of step has its own shape.

ShapeWhat it means
Rounded boxWhere the algorithm starts or stops
Slanted boxInput a value, or output one
RectangleA step that sets or changes a value
DiamondA question with two arrows out, one for each answer

Start at the start box and follow the arrows. Do what each box says. At a diamond, answer its question with the values you have right now, then follow the arrow labelled with that answer.

Finding the loop

A saver puts 15 into a jar each week until the jar holds at least the goal. The goal is a whole number from 1 to 1000. This flowchart works out how many weeks that takes.

StartInput goalsaved = 0weeks = 0saved < goal?saved = saved + 15weeks = weeks + 1Output weeksStop yesno

Look for an arrow that goes back up to a box you have already passed. That arrow makes the loop. Here it leaves weeks = weeks + 1 and goes back up to the diamond.

Now name the parts:

  • The diamond the back arrow returns to holds the loop's condition: saved < goal?.
  • The body is the boxes on the way from the diamond round to the back arrow: saved = saved + 15 and weeks = weeks + 1. They run once on every turn.
  • The way out is the diamond's other arrow. Here it is the "no" arrow, which leads to Output weeks.
  • The boxes before the diamond, saved = 0 and weeks = 0, run once, before the first check. The boxes after the way out run once, after the loop ends.

Always read the labels on a diamond's arrows. Here "yes" means go round again. Another flowchart could ask saved >= goal? instead, and then "yes" would be the way out.

Where the update sits

The update is the box in the body that changes a value the diamond asks about. Here the diamond asks about saved and goal, and only saved = saved + 15 changes one of them. That box is the update.

weeks = weeks + 1 is in the body too, but it is a counter: the diamond never asks about weeks. If the update box were missing, saved would stay at 0, the answer would be yes every time, and the loop would never end.

Tracing it

Trace a flowchart the way you trace code. Keep a column for each value and one for the output, and add a row each time a box sets a value or outputs one. Each time you reach the diamond, answer its question with the values in the table at that moment.

With the input 40:

savedweeksoutput
0
0
15
1
30
2
45
3
3

The diamond is reached 4 times. With saved at 0, 15 and 30, saved < goal? is yes, so the body runs. With saved at 45 it is no, so the arrow leads out to the output box, which outputs 3. The body ran 3 times, one fewer than the number of checks, just as for a while loop in code.

The same loop in code

The flowchart is a while loop. The diamond is the condition, the body boxes are the lines inside the loop, and the back arrow is the end of the body, where the program goes back to check again.

goal = int(input())
saved = 0
weeks = 0
while saved < goal:
    saved = saved + 15
    weeks = weeks + 1
print(weeks)
import java.util.Scanner;

public static void main(String[] args) {
    Scanner scanner
        = new Scanner(System.in);
    int goal = Integer.parseInt(
        scanner.nextLine());
    int saved = 0;
    int weeks = 0;
    while (saved < goal) {
        saved = saved + 15;
        weeks = weeks + 1;
    }
    System.out.println(weeks);
}

The diamond comes before the body, as the while line comes before the lines inside it. So if the first answer is already the way out, the body runs 0 times.

Saying what it does

A question may ask what an algorithm does. Retelling each box ("it adds 15 to saved, then adds 1 to weeks") does not answer that. Trace it with two or three inputs, look at what comes out, and say it in the words of the problem.

InputOutput
403
453
464

So: it outputs how many weeks of saving 15 a week it takes for the jar to hold at least the goal.

Now try it yourself on the questions below.

Exercise 3.9.13.9.1 · Answer itFlowchart odds

The flowchart below describes an algorithm. It reads a whole number n, then repeats some steps.

Trace the flowchart with the input 6. Keep a trace table with a column for i, one for total and one for the output.

(a) Write down every value i has each time total = total + i runs. [1]

(b) Write down every value total takes, in order. [1]

(c) State the output. [1]

(d) State how many times the question in the diamond is asked. [1]

[4]

StartInput ntotal = 0i = 1i <= n?total = total + ii = i + 2Output totalStop yesno

Exercise 3.9.23.9.2 · Answer itFlowchart power of two

The flowchart below describes an algorithm. It reads a whole number n that is 1 or more.

(a) State the output when the input is 20. [1]

(b) State the output when the input is 1. [1]

(c) Identify the box that is the loop's update: the box that changes what the diamond asks about. [1]

(d) Describe what the algorithm outputs, for any input of 1 or more. [1]

[4]

StartInput np = 1p * 2 <= n?p = p * 2Output pStop yesno