Back to Topics
Term 3 DI10-3

Algorithm Design

Learning Objectives

  • Understand what an algorithm is and why they matter
  • Represent algorithms using flowcharts
  • Write algorithms in pseudocode
  • Apply sequence, selection, and iteration constructs

What is an Algorithm?

An algorithm is a step-by-step set of instructions to solve a problem or complete a task. Algorithms are the foundation of all computer programs.

Real-life example: A recipe is an algorithm. It has a sequence of steps, decisions ("if the sauce is too thick, add water"), and repetition ("stir for 5 minutes").

Properties of Good Algorithms

  • Clear - Unambiguous instructions that can be followed
  • Finite - Must eventually end (not run forever)
  • Effective - Each step can actually be performed
  • Input - Takes zero or more inputs
  • Output - Produces at least one result

Control Structures

All algorithms are built using three fundamental control structures:

Sequence

Steps executed one after another, in order.

Step 1
Step 2
Step 3

Selection

Making decisions based on conditions.

IF condition THEN
  do this
ELSE
  do that

Iteration

Repeating steps (loops).

WHILE condition
  do something
ENDWHILE

Flowcharts

A flowchart is a visual representation of an algorithm using standard symbols.

Flowchart Symbols

Symbol Name Purpose
Terminator Start or End of the algorithm
Process An action or calculation
Decision Yes/No question (branching)
Input/Output Getting input or showing output
Connector Links parts of flowchart
Flow Line Shows direction of flow

Flowchart Example

Algorithm to check if a number is positive, negative, or zero:

Start
Input: number
number > 0?
Yes
Output: "Positive"
No
number < 0?
End

Pseudocode

Pseudocode is a way to describe an algorithm using structured English. It's not a real programming language but is easy to translate into code.

Pseudocode Conventions

  • Keywords in CAPITALS: IF, THEN, ELSE, WHILE, FOR, INPUT, OUTPUT
  • Use indentation to show structure
  • One instruction per line
  • Use meaningful variable names

Selection in Pseudocode

// Simple IF
IF age >= 18 THEN
    OUTPUT "You can vote"
ENDIF

// IF-ELSE
IF temperature > 30 THEN
    OUTPUT "It's hot!"
ELSE
    OUTPUT "It's comfortable"
ENDIF

// IF-ELSEIF-ELSE
IF score >= 80 THEN
    grade = "A"
ELSEIF score >= 60 THEN
    grade = "B"
ELSEIF score >= 40 THEN
    grade = "C"
ELSE
    grade = "D"
ENDIF

Iteration in Pseudocode

// WHILE loop (condition checked first)
count = 0
WHILE count < 5
    OUTPUT count
    count = count + 1
ENDWHILE

// FOR loop (known number of iterations)
FOR i = 1 TO 10
    OUTPUT i
NEXT i

// FOR EACH (iterate through a collection)
FOR EACH item IN shoppingList
    OUTPUT item
NEXT item

Example: Finding the Maximum

Algorithm to find the largest number in a list:

BEGIN FindMaximum
    INPUT numbers[]
    max = numbers[0]

    FOR EACH num IN numbers
        IF num > max THEN
            max = num
        ENDIF
    NEXT num

    OUTPUT "The maximum is: " + max
END

Trace Tables

A trace table tracks how variable values change as an algorithm executes. It helps verify that an algorithm works correctly.

Trace table for counting from 1 to 3:

Step count count < 4 Output
1 1 TRUE 1
2 2 TRUE 2
3 3 TRUE 3
4 4 FALSE -

Key Terminology

  • Algorithm - Step-by-step instructions to solve a problem
  • Flowchart - Visual representation of an algorithm
  • Pseudocode - Structured English description of an algorithm
  • Sequence - Steps executed in order
  • Selection - Making decisions (IF statements)
  • Iteration - Repeating steps (loops)
  • Trace table - Tracking variable values through execution
🏠

Project Connection

In Simpson's House...

The Python code running on the Raspberry Pi is an algorithm. You can draw it as a flowchart, write it as pseudocode, and trace it step by step — it uses every control structure you learn here: sequence, selection, and iteration.

Selection (IF statements)

The on_message() handler branches on topic and payload

The core logic is a chain of IF statements: IF topic == "home/light" AND payload == "ON" THEN set GPIO pin HIGH. This is selection — flowchart it as diamond shapes branching left and right.

Iteration

loop_forever() is a WHILE loop that never ends

client.loop_forever() keeps the program running indefinitely, processing incoming messages. In pseudocode: WHILE system is running → wait for message → handle it → ENDWHILE.

Flowcharting the System

Draw the complete message-handling algorithm

Start → Connect to broker → Subscribe to topics → [WAIT for message] → Decode topic → Decision: home/light? home/garage? home/door? → Branch to action → Loop back. A real, traceable flowchart.

Trace Tables

Trace the algorithm with a specific input

Trace: topic = "home/light", payload = "ON". Step 1: decode payload → "ON". Step 2: topic matches "home/light" → TRUE. Step 3: payload == "ON" → TRUE. Step 4: GPIO.output(17, HIGH). Output: LED on.

Explore the full Simpson's House project →