pseudocode using the three basic constructs of sequence, decision, and iteration
9.2 Algorithms
Pseudocode Basics
Pseudocode is a simple, human‑readable way to describe how an algorithm works. It uses three core constructs:
- Sequence – steps executed one after another.
- Decision – choose between two or more paths (if‑then‑else).
- Iteration – repeat a block of steps (for, while).
Analogy: Think of a recipe. The sequence is the order of ingredients, the decision is “if you want a spicy version, add chili”, and the iteration is “stir for 5 minutes”.
Sequence Example
1. Read input number n 2. Set sum = 0 3. sum = sum + n 4. Output sum
Decision Example
1. Read temperature T 2. IF T > 30 THEN 3. Output "Hot day" 4. ELSE 5. Output "Cool day" 6. ENDIF
Iteration Example
1. Read n 2. FOR i = 1 TO n DO 3. Output i 4. END FOR
Combining Constructs
Algorithms often mix all three constructs. Below is a classic example: computing the factorial of a number.
| Step | Pseudocode |
|---|---|
| 1 | Read n |
| 2 | Set fact = 1 |
| 3 | FOR i = 1 TO n DO |
| 4 | fact = fact * i |
| 5 | END FOR |
| 6 | Output fact |
Exam Tip: When asked to write pseudocode, always start with a clear input and output section. Use IF for decisions and FOR/WHILE for loops. Keep the syntax consistent and avoid unnecessary variables.
Common Mistakes to Avoid
- Using real programming language syntax (e.g., semicolons, braces).
- Omitting the END IF or END FOR statements.
- Not clearly indicating the start and end of loops.
- Mixing up FOR and WHILE when the condition is not a simple counter.
Quick Practice Problem
Write pseudocode to find the largest number in a list of m integers.
- Read m and the list of numbers.
- Set max = first number.
- FOR each remaining number x DO
- IF x > max THEN max = x
- END FOR
- Output max
Exam Tip: Remember to initialise max before the loop and to update it only when a larger value is found. This ensures the algorithm works for negative numbers as well.
Emoji Summary
- 🔢 Sequence – steps in order.
- ❓ Decision – choose a path.
- 🔄 Iteration – repeat steps.
- ?? Result – final output.
Revision
Log in to practice.
2 views
0 suggestions