Draw a flowchart from pseudocode

Published by Patrick Mutisya · 14 days ago

Cambridge A-Level Computer Science 9618 – 9.2 Algorithms: Drawing a Flowchart from Pseudocode

9.2 Algorithms – Drawing a Flowchart from Pseudocode

Learning Objective

By the end of this lesson you will be able to translate a given piece of pseudocode into a correct flowchart using standard flowchart symbols.

Why Flowcharts?

Flowcharts provide a visual representation of an algorithm, making it easier to:

  • Understand the logical sequence of operations.
  • Identify potential errors or inefficiencies.
  • Communicate algorithms to others who may not be familiar with the specific pseudocode syntax.

Standard Flowchart Symbols

SymbolNamePurpose
ProcessPerforms a calculation or assignment.
Start/EndMarks the beginning or termination of the algorithm.
Input/OutputReads data from the user or displays results.
DecisionTests a condition; leads to two possible branches (Yes/No).
ConnectorJoins separate parts of the flowchart when space constraints require it.

Step‑by‑Step Conversion Process

  1. Read the pseudocode carefully and identify the logical blocks (initialisation, input, loops, decisions, output).
  2. Map each block to the appropriate flowchart symbol:

    • Assignments and calculations → Process.
    • Reading or printing data → Input/Output.
    • Conditional statements (IF, ELSE) → Decision.
    • Repeated actions (FOR, WHILE) → Decision combined with Process to form a loop.

  3. Place a Start symbol at the top and an End symbol at the bottom.
  4. Connect the symbols with arrows in the order they are executed.
  5. For nested structures, indent the flowchart vertically to keep the hierarchy clear.
  6. Check the flowchart against the original pseudocode to ensure every line is represented.

Example Pseudocode

Consider the following pseudocode that calculates the factorial of a positive integer n:

INPUT n

IF n < 0 THEN

OUTPUT "Invalid input"

ELSE

SET fact ← 1

SET i ← 1

WHILE i ≤ n DO

SET fact ← fact × i

SET i ← i + 1

END WHILE

OUTPUT fact

END IF

Translating the Example into a Flowchart

Follow the conversion steps:

  1. Start symbol.
  2. Input symbol for n.
  3. Decision symbol to test n < 0.

    • If Yes → Output “Invalid input” → End.
    • If No → Continue to initialise fact and i (Process symbols).

  4. Decision symbol for the loop condition i ≤ n.

    • If Yes → Process symbol to compute fact ← fact × i, then another Process to increment i. Return to loop condition.
    • If No → Output fact (Output symbol) → End.

  5. End symbol.

Suggested Flowchart Layout

Suggested diagram: Flowchart representing the factorial algorithm described above, using the standard symbols listed in the table.

Key Points to Remember

  • Every line of pseudocode must appear in the flowchart, either as a Process, Decision, or Input/Output symbol.
  • Loops are always represented by a Decision symbol that feeds back to an earlier point in the chart.
  • Maintain a clear top‑to‑bottom flow; avoid crossing arrows where possible.
  • Label each arrow with “Yes”/“No” or “True”/“False” when exiting a Decision symbol.

Practice Exercise

Convert the following pseudocode into a flowchart. Use the steps and symbols described above.

INPUT a, b

IF a = b THEN

OUTPUT "Numbers are equal"

ELSE

IF a > b THEN

SET max ← a

SET min ← b

ELSE

SET max ← b

SET min ← a

END IF

OUTPUT "Maximum = ", max

OUTPUT "Minimum = ", min

END IF

When you have completed the flowchart, compare it with a peer’s version to check for consistency.