Welcome to Representing Algorithms!

Hello future Computer Scientists! In the previous chapter, we learned that an algorithm is just a set of instructions used to solve a problem (like a detailed recipe!).
But how do we write these instructions down so that a human programmer or the computer itself can understand them perfectly?
That’s exactly what this chapter is about! We are going to explore the two main ways we represent algorithms: Pseudocode and Flowcharts.

Why is this important? If you can clearly represent an algorithm, you are halfway to writing successful, bug-free computer code!


1. Structured English (Pseudocode)

What is Pseudocode?

Pseudocode (pronounced "SUE-doh-code") means "fake code." It is a way of writing down an algorithm using a mixture of everyday English words and standard programming keywords.
It is not a real programming language, but it follows the logical structure of one.

The goal of pseudocode is to be easily understood by humans, but formal enough that a programmer can translate it directly into Python, Java, or any other language.

Key Features of Good Pseudocode:
  • Uses common programming terms (e.g., INPUT, OUTPUT, IF, WHILE).
  • Uses indentation (spacing) to show structure and blocks of code.
  • It must be unambiguous (only one possible meaning).

Memory Aid: Think of Pseudocode as the rough draft or the blueprint of your actual program.

The Three Fundamental Structures of Algorithms

All algorithms, no matter how complex, are built using three fundamental structures. You must be able to represent all three in pseudocode.

A. Sequence (Step-by-Step Order)

Sequence is the most basic structure. It simply means performing instructions one after the other, in the order they are written.
Example: Making a cup of tea. Boil water, put tea bag in cup, pour water, add milk.

Pseudocode Example (Sequence):

1. INPUT student_name
2. INPUT score_1
3. INPUT score_2
4. total_score = score_1 + score_2
5. OUTPUT total_score

B. Selection (Decision Making)

Selection (or branching) allows the algorithm to make a decision based on a condition (a true/false question). This is usually achieved using the IF...THEN...ELSE structure.

Analogy: When you get dressed, you use selection:
IF (it is raining) THEN wear a coat, ELSE wear a light jacket.

Pseudocode Example (Selection):

INPUT age
IF age >= 18 THEN
    OUTPUT "You are eligible to vote"
ELSE
    OUTPUT "You are not yet eligible"
ENDIF

Important Note: Always remember to close your structure with ENDIF. Indentation is crucial for readability!

C. Iteration (Repetition or Looping)

Iteration means repeating a set of instructions until a certain condition is met.
This saves you from writing the same code many times.

The two main types of iteration you will see in GCSE are:
1. Count-controlled (FOR loop): Repeats a set number of times.
2. Condition-controlled (WHILE loop): Repeats as long as a condition is TRUE.

Pseudocode Example (WHILE Loop - Condition-controlled):

password = ""
WHILE password != "secret"
    INPUT password
ENDWHILE
OUTPUT "Access Granted"

Pseudocode Example (FOR Loop - Count-controlled):

FOR counter = 1 TO 5
    OUTPUT "Hello!"
NEXT counter


Quick Review: Pseudocode Keywords

  • INPUT/OUTPUT: Getting data in, showing results out.
  • IF/THEN/ELSE/ENDIF: Selection (decisions).
  • WHILE/ENDWHILE: Repeating based on a condition.
  • FOR/TO/NEXT: Repeating a set number of times.


2. Flowcharts

What is a Flowchart?

A flowchart is a visual way to represent an algorithm using standard shapes and connecting arrows. It is often easier to see the structure and flow of control (how the steps link together) using a flowchart compared to reading text.

Did you know? Flowcharts were first used decades ago, long before modern programming languages, to map out business processes and logic!

Essential Flowchart Symbols

You must know the purpose and shape of the following five core symbols:

1. The Terminator (Start/Stop)

Shape: Oval or elongated rectangle.
Purpose: Marks the beginning (START) and the end (STOP) of the algorithm. Every flowchart must have one START and one STOP.

2. The Process Box

Shape: Rectangle.
Purpose: Represents an action, calculation, or instruction that changes data (e.g., setting a variable, calculating a total).
Example: Total = Price * Quantity

3. Input/Output Box

Shape: Parallelogram (slanted sides).
Purpose: Represents data entering the system (INPUT) or data being displayed/printed (OUTPUT).
Example: Input Name or Output Result

4. The Decision Box (Selection)

Shape: Diamond.
Purpose: Represents a condition or question that must be answered with YES/TRUE or NO/FALSE. This is where selection (IF...THEN...ELSE) occurs.
Key Rule: Decision boxes always have one arrow entering and two arrows leaving (one for YES, one for NO).

5. Flow Lines

Shape: Arrows.
Purpose: Connects the shapes and shows the exact direction or path the algorithm follows.

Memory Trick for Flowchart Shapes:
The Oval ends the process (O for Oval/Out).
The Parallelogram is for Putting data in or out.
The Rectangle is for Routine calculations (Process).
The Diamond makes a Decision.

Building a Simple Flowchart (Example)

Let's represent the simple sequence of asking for a score and checking if it’s high enough (Selection).

(1) Oval: START
(2) Arrow down to
(3) Parallelogram: Input Score
(4) Arrow down to
(5) Diamond: Is Score > 50?
     (a) Arrow labelled 'YES' to Parallelogram: Output "Pass"
     (b) Arrow labelled 'NO' to Parallelogram: Output "Fail"
(6) Arrows from both "Pass" and "Fail" rejoin and lead to
(7) Oval: STOP

Don't worry if drawing it seems tricky at first. Focus on the symbols and their purpose. If you connect the wrong shapes, the logic will break! For instance, never draw an arrow leading out of a Process box and immediately back into the same box without a Decision diamond in between, as that would be an infinite loop!


3. Comparing Representations

Both pseudocode and flowcharts are excellent tools for designing algorithms before you write the final code.

Representation Advantages Disadvantages
Pseudocode
  • Easy to translate directly into real code.
  • Handles very complex structures and long algorithms easily.
  • Less time consuming to write than drawing a flowchart.
  • Logic flow (the path) is not as immediate or clear as a visual diagram.
  • Errors in logic can sometimes be missed when just reading text.
Flowcharts
  • Excellent visual representation of the flow of control.
  • Easy to spot complex loops or illogical sequences.
  • Good for explaining the algorithm to non-programmers.
  • Can become very large, complicated, and hard to manage for huge algorithms.
  • Can be slow to draw accurately.

Key Takeaways for Representation

The way we represent an algorithm does not change the algorithm itself, only how we communicate it. Ensure your chosen method clearly shows:
1. Sequence: The fixed order of steps.
2. Selection: All decision points (IF/Diamond).
3. Iteration: All points where the algorithm loops (FOR/WHILE/Diamond creating a loop).


Keep practicing translating simple problems into both pseudocode and flowcharts, and you'll master this core skill!