08-flow-diagrams.py
python-programming / programming-constructs  ·  Lesson 08
Lesson 08 — Programming constructs

Flow Diagrams

Represent an algorithm visually using standard symbols and connected paths.

A flow diagram, also called a flowchart, shows how an algorithm moves from one step to the next. The shapes tell you what kind of step is happening, while the connecting lines show the route through the algorithm.

Predict

Follow the diagram before running any code. For each input, predict which message will be produced.

even-or-odd.flow
Start INPUT number number % 2 == 0 TrueFalse OUTPUT "Even" OUTPUT "Odd" End
Predict before running
  • What is output if the input is 14?
  • What is output if the input is 19?
  • What does the diamond decide?
  • Why must both paths eventually reach End?

Run

Now run the Python version. Try the same values you used in your prediction.

even-or-odd.py
number = int(input("Number: "))

if number % 2 == 0:
    print("Even")
else:
    print("Odd")
Compare prediction with result
  • Run the program with 14 and 19.
  • Does the result match the path you followed in the diagram?
  • Try one more even number and one more odd number.

Investigate

Compare this program with its flow diagram. Explain how each Python statement is represented visually.

username-check.py
username = input("Username: ")
characters = len(username)

if characters >= 5:
    print("Accepted")
else:
    print("Too short")
username-check.flow
Start INPUT username characters = length of username characters >= 5 TrueFalse OUTPUT "Accepted" OUTPUT "Too short" End
Investigate the representation
  • Which shape represents the call to input()?
  • Why is characters = len(username) a process rather than a decision?
  • Which Python expression appears inside the decision diamond?
  • What do the True and False labels tell you?
  • How are the two print() statements represented?

The standard symbols

Use the symbol that matches the purpose of each step. A reader should be able to recognise the type of operation from the shape alone.

SymbolNameUse
LineConnects one step to the next. Arrowheads can make the direction explicit.
Input / OutputReceives data into the algorithm or sends data out.
ProcessPerforms an action such as an assignment or calculation.
DecisionTests a condition and provides two possible paths, normally labelled True/False or Yes/No.
SubprogramCalls a named subprogram whose detailed steps are defined elsewhere.
TerminalMarks the Start or End of the algorithm.
Decision pathsA decision has two outcomes. Label both paths clearly so there is no ambiguity about which route is followed when the condition is true or false.

Sequence, selection and iteration

There is no separate “loop” symbol. Iteration is created by connecting a path back to an earlier decision. The three examples below show the main patterns.

Sequence
Start INPUT width, height area = width × height OUTPUT area End
Selection
Start score >= 50 TrueFalse OUTPUT "Pass" OUTPUT "Retry" End
Iteration
Start count = 1 count <= 3 TrueFalse OUTPUT count count = count + 1 End

From a flow diagram to Python

Flow diagram ideaTypical Python equivalent
Inputinput(), often followed by casting
Outputprint()
ProcessAn assignment or calculation, for example total = price * quantity
DecisionA Boolean condition used by selection or iteration
SubprogramA call to a named procedure-style subprogram or function
LoopA path that returns to a decision while repetition is still required

Checking a flow diagram

Use this checklist
  • Start and End use terminal shapes.
  • Inputs and outputs use parallelograms.
  • Assignments and calculations use process rectangles.
  • Conditions use decision diamonds.
  • Both decision paths are labelled clearly.
  • Every shape is connected into the algorithm and every possible route leads onwards.
  • Loops return to the correct decision instead of creating an unconnected path.
Focus on the algorithmA flow diagram represents the logic of the solution. It does not need Python punctuation or indentation, but its conditions, calculations, inputs and outputs must be unambiguous.

Self-study guide

A flow diagram represents an algorithm visually. The meaning comes from using the correct standard symbols and connecting them in the correct order. It should show what the algorithm does without depending on Python syntax.

OCR expects the standard symbols for Line, Input/Output, Process, Decision, Sub program and Terminal. A decision creates alternative paths; iteration is shown by a path that returns to an earlier decision rather than by a special “loop” symbol.

OCR focusRecognise and construct flow diagrams using the standard OCR symbols. Be able to translate between a visual algorithm and working code.

OCR Exam Reference Language ↔ Python

OCR Exam Reference Language / termPythonWhat to remember
TerminalStart / EndUse a terminal symbol for the beginning and end of the algorithm.
Input / Outputinput() / print()Keyboard input and displayed output.
Processassignment / calculationFor example total = price * quantity.
Decisionif / elif / else or loop conditionA Boolean question creates branches.
Sub programfunction/procedure callRepresents a call to a named subprogram.
Lineflow of executionArrowed connections show the order and direction.
Read both formsIn an examination you may see OCR Exam Reference Language even when you have learned the idea in Python. Translate the structure, not just the symbols.

Worked example

The following Python selection corresponds to a Start → Input → Decision → Output → End flow.

lesson-08-worked.py
age = int(input("Age: "))

if age >= 13:
    print("Allowed")
else:
    print("Not allowed")
Output
Allowed
Work through it line by line
  • Start with a Terminal symbol.
  • input() is represented by an Input/Output symbol.
  • age >= 13? belongs in a Decision symbol.
  • The two branches should be clearly labelled, for example Yes/No or True/False.
  • Each output uses an Input/Output symbol.
  • The paths can rejoin before the final End terminal.

Another example

Iteration uses the same decision symbol, but one path returns to repeat part of the algorithm.

lesson-08-example-2.py
count = 1

while count <= 3:
    print(count)
    count = count + 1
Output
1
2
3
Why this example matters
  • The decision asks count <= 3?.
  • The True/Yes branch performs the output and update.
  • The flow then returns to the decision.
  • The False/No branch leaves the loop and continues to End.

Common mistakes

Avoid these errors
  • Inventing a special loop symbol instead of using a decision plus a return path.
  • Using a Process symbol for input or output.
  • Leaving decision branches unlabelled so the routes are ambiguous.
  • Forgetting the Sub program symbol when the algorithm calls a procedure or function.
Exam-safe warningA flow diagram is language-independent. Do not turn it into a picture of Python syntax; represent the algorithmic actions with the correct symbols.

Exam-ready checklist

You should now be able to
  • Choose the correct symbol for each action.
  • Construct sequence, selection and iteration using flow-diagram conventions.
  • Translate a flow diagram into code and code into a flow diagram.
  • Use a Sub program symbol for a subprogram call.
  • Label decision branches clearly.

What you have learned

  • Flow diagrams use standard shapes to represent different kinds of algorithm step.
  • A decision has two clearly labelled paths.
  • Sequence, selection and iteration can all be represented visually.
  • The same algorithm can be expressed as a flow diagram or as program code.