Write pseudocode from a flowchart

Resources | Subject Notes | Computer Science

A-Level Computer Science 9.2 Algorithms - Pseudo-code from Flowchart

A-Level Computer Science 9.2 Algorithms

Objective: Write pseudo-code from a flowchart

This section focuses on translating a flowchart into a step-by-step, human-readable algorithm using pseudo-code. Pseudo-code is an informal way of describing an algorithm, using a combination of natural language and structured elements.

Understanding Flowcharts and Pseudo-code

A flowchart visually represents the flow of an algorithm using symbols. Pseudo-code provides a textual equivalent, making the algorithm easier to understand and implement in any programming language.

Key Pseudo-code Constructs

  • START / BEGIN: Marks the beginning of the algorithm.
  • INPUT: Represents reading data from the user or a source.
  • OUTPUT: Represents displaying data to the user or a destination.
  • Process: Represents a calculation or operation.
  • IF: Represents a conditional statement.
  • ELSE: Represents the alternative block in a conditional statement.
  • WHILE: Represents a loop that continues as long as a condition is true.
  • ENDIF: Marks the end of an IF or ELSE block.
  • ENDWHILE: Marks the end of a WHILE loop.
  • RETURN: Represents the end of an algorithm or a specific section.

Example: Translating a Flowchart to Pseudo-code

Consider the following flowchart:

Here's the corresponding pseudo-code:

Step Pseudo-code
1. START BEGIN
2. Input INPUT number
3. Process result = number * 2
4. Output OUTPUT result
5. END END

In this example:

  • The flowchart starts with a START.
  • It then INPUTs a number.
  • A Process is performed: the number is multiplied by 2 and the result is stored in result.
  • The OUTPUT result to the user.
  • Finally, the algorithm ENDs.

More Complex Example: Finding the Maximum of Two Numbers

Let's consider a flowchart for finding the maximum of two numbers:

The pseudo-code for this flowchart is as follows:

Step Pseudo-code
1. START BEGIN
2. Input INPUT number1
3. Input INPUT number2
4. Process IF number1 > number2 THEN
5. Process maximum = number1
6. Else maximum = number2
7. ENDIF
8. Output OUTPUT maximum
9. END END

Explanation:

  • The algorithm BEGINs.
  • It takes two INPUTs, number1 and number2.
  • An IF statement checks if number1 is greater than number2.
  • If it is, number1 is assigned to maximum.
  • Otherwise, number2 is assigned to maximum.
  • The maximum value is then OUTPUTed.
  • Finally, the algorithm ENDs.