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

  1. Use clear, descriptive names – e.g., totalScore instead of t.
  2. Indent consistently – 2–4 spaces per level.
  3. Write in plain English – avoid language‑specific syntax.
  4. Use standard control‑flow keywords – IF, THEN, ELSE, FOR, WHILE, REPEAT.
  5. Keep loops simple – don’t nest too many loops unless necessary.
  6. Modularise – extract repeated logic into SUBROUTINE blocks.
  7. Comment where needed – use // or /* */ to explain tricky parts.
  8. 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.

  1. INPUT list[1…n]
  2. SET max = list[1]
  3. FOR i FROM 2 TO n DO
  4.  IF list[i] > max THEN
  5.   SET max = list[i]
  6.  END IF
  7. END FOR
  8. 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.

2 views 0 suggestions