Write efficient pseudocode
11.3 Structured Programming – Write Efficient Pseudocode
What is Structured Programming?
Structured programming is like following a recipe: you have a clear list of ingredients (variables), a step‑by‑step method (control flow), and you avoid messy “spaghetti” code that’s hard to follow. The main ideas are:
- Use sequential execution – one step after another.
- Use selection (if/else) to choose between alternatives.
- Use iteration (for/while) to repeat a block of steps.
- Encapsulate repeated logic in subroutines (functions).
Why Pseudocode?
Pseudocode lets you write the logic of your program without worrying about the exact syntax of a programming language. Think of it as a “language‑agnostic” recipe that anyone can read.
Guidelines for Writing Efficient Pseudocode
- Use clear, descriptive names – e.g.,
totalScoreinstead oft. - Indent consistently – 2–4 spaces per level.
- Write in plain English – avoid language‑specific syntax.
- Use standard control‑flow keywords – IF, THEN, ELSE, FOR, WHILE, REPEAT.
- Keep loops simple – don’t nest too many loops unless necessary.
- Modularise – extract repeated logic into
SUBROUTINEblocks. - Comment where needed – use
//or/* */to explain tricky parts. - Test with sample data – write a few test cases to check logic.
Exam Tip: Look for the “Control Flow” section in the question.
Exam questions often ask you to write pseudocode for a given problem. Focus on:
- Identifying the input and output.
- Determining the main loop (e.g., for each student, while number < 10).
- Using conditionals to handle special cases.
- Breaking the problem into sub‑tasks and writing subroutines.
Example: Pseudocode for Calculating the Factorial of a Number
| Step | Pseudocode |
|---|---|
| 1 | INPUT n // integer ≥ 0 |
| 2 | SET result = 1 |
| 3 | FOR i FROM 1 TO n DO |
| 4 | SET result = result × i |
| 5 | END FOR |
| 6 | OUTPUT result |
💡 Tip: Always initialise variables before use and keep loops simple.
Example: Pseudocode for Sorting a List (Bubble Sort)
| Step | Pseudocode |
|---|---|
| 1 | INPUT list[1…n] |
| 2 | FOR i FROM 1 TO n-1 DO |
| 3 | FOR j FROM 1 TO n-i DO |
| 4 | IF list[j] > list[j+1] THEN |
| 5 | SWAP list[j] WITH list[j+1] |
| 6 | END IF |
| 7 | END FOR |
| 8 | END FOR |
| 9 | OUTPUT list |
🧩 Note: Bubble sort is easy to understand but not efficient for large lists. In exams, you may be asked to explain its time complexity: $O(n^2)$.
Exam Question Practice
🔍 Task: Write pseudocode to find the largest number in a list of integers.
- INPUT list[1…n]
- SET max = list[1]
- FOR i FROM 2 TO n DO
- IF list[i] > max THEN
- SET max = list[i]
- END IF
- END FOR
- OUTPUT max
??
Tip: Always initialise max with the first element to avoid missing negative numbers.
Final Exam Reminder
- Keep pseudocode simple and readable.
- Use clear variable names and consistent indentation.
- Show control flow explicitly with IF/ELSE and loops.
- Include comments for non‑obvious steps.
- Check for off‑by‑one errors in loops.
Good luck! 🚀
Revision
Log in to practice.