Flowcharts

The standard symbols, and turning a flowchart into code.

F5.2AlgorithmsGCSE15 min

Do this lesson in the simulator

A flowchart draws an algorithm. Each step is a box, arrows show the order, and a diamond shows a decision. Flowcharts are good for seeing the shape of an algorithm at a glance: where it loops, where it branches, and where it ends. Exams use them a lot, both to give you an algorithm and to ask you to draw one.

The symbols

The five flowchart symbolsStart / EndProcessDecisionInput / OutputSubroutine
The five flowchart symbols
Symbol Used for Example
Rounded box Start or end Start, End
Rectangle A process: do something drive forward 5 cm, count = count + 1
Diamond A decision: a yes or no question, with two arrows out Is the distance more than 25?
Slanted box Input or output input a password, output "arrived"
Box with double sides A subroutine: a subprogram defined elsewhere tone(note, 0.2)

Arrows join the symbols and show which way the algorithm goes. A decision always has exactly two arrows out, labelled Yes and No.

From flowchart to code

Here is creep to the wall as a flowchart:

Flowchart: creep to the wallStartdistance()> 25?Drive forward 5 cmOutput "arrived"EndYesNo
Flowchart: creep to the wall

The arrow from the bottom back up to the decision makes a loop. A decision that an arrow loops back to becomes a while: keep going round while the answer is Yes, leave when it is No.

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

while distance() > 25:          # the decision
    forward(50, distance=5)     # the process on the Yes path
print("arrived")                # the output on the No path

Run this in the simulator

A decision with no loop back becomes an if. So: arrow back to an earlier decision, while; no arrow back, if.

A loop that counts

This flowchart plays a rising scale of five notes. The subroutine box is a call to tone, which is defined elsewhere, in the robot's library.

Flowchart: a rising scaleStartnote = 300count = 0count < 5?tone(note, 0.2)note = note + 100count = count + 1Output countEndYesNo
Flowchart: a rising scale
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

note = 300
count = 0
while count < 5:
    tone(note, 0.2)
    note = note + 100
    count = count + 1
print(count)

Run this in the simulator

Follow the arrows with your finger and check each line matches a box. A flowchart has no for: a counting loop is drawn with a variable, a decision and an update, exactly as a while does it.

Drawing your own

To draw a flowchart for an algorithm:

  1. Start with a Start box and end with an End box.
  2. Put each action in its own rectangle, and each input or output in a slanted box.
  3. Every question is a diamond with a Yes arrow and a No arrow.
  4. For a loop, draw the arrow back to the decision that controls it.
  5. Check it: follow it for a real example, the way the computer would.

Task: the password guard

Turn this flowchart into a program. Ask for a password with the prompt Password?. While it is not bugbot, print wrong, beep, and ask again. When it is right, turn the LED green and drive forward 20 cm. The task types robot first, then bugbot.

Flowchart: the password guardStartInput passwordpassword =="bugbot"?LED greendrive 20 cmEndOutput "wrong"BeepYesNo
Flowchart: the password guard
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

password = input("Password? ")

Challenges

  1. Draw a flowchart for the parking sensor from lesson F2.8.
  2. Draw a flowchart for an algorithm that asks for two numbers and outputs the larger.
  3. Change the guard so it gives up after three wrong passwords. What must be added to the flowchart?