Flowcharts
The standard symbols, and turning a flowchart into code.
Do this lesson in the simulatorA 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
| 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:
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
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.
# 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)
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:
- Start with a Start box and end with an End box.
- Put each action in its own rectangle, and each input or output in a slanted box.
- Every question is a diamond with a Yes arrow and a No arrow.
- For a loop, draw the arrow back to the decision that controls it.
- 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.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
password = input("Password? ")
Challenges
- Draw a flowchart for the parking sensor from lesson F2.8.
- Draw a flowchart for an algorithm that asks for two numbers and outputs the larger.
- Change the guard so it gives up after three wrong passwords. What must be added to the flowchart?