Cambridge A-Level Computer Science 9618 – 9.2 Algorithms: Drawing a Flowchart from Pseudocode9.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
| Symbol | Name | Purpose |
|---|
| ▭ | Process | Performs a calculation or assignment. |
| ◯ | Start/End | Marks the beginning or termination of the algorithm. |
| ▱ | Input/Output | Reads data from the user or displays results. |
| ◇ | Decision | Tests a condition; leads to two possible branches (Yes/No). |
| ⬭ | Connector | Joins separate parts of the flowchart when space constraints require it. |
Step‑by‑Step Conversion Process
- Read the pseudocode carefully and identify the logical blocks (initialisation, input, loops, decisions, output).
- 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.
- Place a Start symbol at the top and an End symbol at the bottom.
- Connect the symbols with arrows in the order they are executed.
- For nested structures, indent the flowchart vertically to keep the hierarchy clear.
- 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:
- Start symbol.
- Input symbol for
n. - Decision symbol to test
n < 0.- If Yes → Output “Invalid input” → End.
- If No → Continue to initialise
fact and i (Process symbols).
- 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.
- 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.