Write algorithms with decision-making (branching, looping)
4 Algorithms and Flowcharts
What is an Algorithm?
An algorithm is a step‑by‑step recipe that solves a problem or performs a task. Think of it like a cooking recipe: you follow the instructions in order to get a tasty dish. In computing, the recipe is written in plain language or a diagram.
Key properties:
- Well‑defined steps
- Finite number of steps
- Input and output
- Deterministic (same input gives same output)
Flowcharts – Visualising Algorithms
Flowcharts use shapes to represent actions:
- Oval – Start/End
- Rectangle – Process/Instruction
- Diamond – Decision (branch)
- Parallelogram – Input/Output
Example: Check if a number is even or odd
- Start
- Input number $n$
- Decision: Is $n \bmod 2 = 0$?
- If yes → Output “Even”
- If no → Output “Odd”
- End
🧩 Flowcharts help you spot errors before coding.
Decision-Making (Branching)
Branching lets the algorithm choose a path based on a condition.
Syntax in pseudocode:
IF condition THEN
// do something
ELSE
// do something else
END IF
Example: Grade a test
- Input score $s$
- IF $s \ge 90$ THEN Grade = A
- ELSE IF $s \ge 80$ THEN Grade = B
- ELSE IF $s \ge 70$ THEN Grade = C
- ELSE Grade = F
🔀 Remember: each IF can have an ELSE IF or ELSE.
Looping (Repetition)
Loops repeat a block of code while a condition holds.
Common loop types:
- FOR – fixed number of iterations
- WHILE – until condition is false
- DO‑WHILE – execute at least once
Example: Sum the first 10 natural numbers
sum = 0
FOR i = 1 TO 10
sum = sum + i
END FOR
// sum = 55
🔄 Loops are like a conveyor belt that keeps moving items until a stop signal.
Putting It All Together – A Mini‑Project
Write an algorithm to find the largest of three numbers.
- Start
- Input $a$, $b$, $c$
- Set $max = a$
- IF $b > max$ THEN $max = b$
- IF $c > max$ THEN $max = c$
- Output $max$
- End
🛠️ Test with different inputs to ensure it works.
Exam Tips
| Tip | Why It Helps |
|---|---|
| Use clear labels (Start, End, Decision) | Reduces confusion for examiners. |
| Show all steps in loops (initialisation, condition, update) | Demonstrates understanding of loop mechanics. |
| Use pseudocode before drawing flowchart | Helps organise thoughts and catch errors early. |
| Check edge cases (e.g., zero, negative numbers) | Shows thoroughness and robustness. |
📚 Practice by writing algorithms for everyday tasks – like making a sandwich or planning a school trip. The more you practice, the easier it becomes!
Revision
Log in to practice.