pseudocode using the three basic constructs of sequence, decision, and iteration

Published by Patrick Mutisya · 14 days ago

Cambridge A-Level Computer Science 9618 – Topic 9.2 Algorithms

9.2 Algorithms

Objective

To understand how to write clear pseudocode using the three basic programming constructs: sequence, decision, and iteration.

1. What is an Algorithm?

An algorithm is a finite, well‑defined set of instructions that solves a problem or performs a computation. It must be:

  • Unambiguous – each step is clear.
  • Finite – it terminates after a limited number of steps.
  • Effective – each step can be carried out in a reasonable amount of time.

2. The Three Basic Constructs

All algorithms can be built from three fundamental control structures. In pseudocode they are expressed as follows:

ConstructPurposePseudocode Example
SequenceExecute statements one after another.

READ a

READ b

SET sum ← a + b

WRITE sum

Decision (Selection)Choose between alternative paths.

IF a > b THEN

SET max ← a

ELSE

SET max ← b

ENDIF

Iteration (Repetition)Repeat a block of statements.

SET i ← 1

WHILE i ≤ n DO

WRITE i

SET i ← i + 1

ENDWHILE

3. Pseudocode Conventions

  • Keywords are written in upper case (e.g., IF, WHILE, ENDWHILE).
  • Indentation shows the block structure.
  • Variables are given meaningful names; type is not required.
  • Mathematical symbols may be written using LaTeX, e.g., \$a \le b\$, \$i \leftarrow i + 1\$.
  • Input and output are represented by READ and WRITE (or INPUT/OUTPUT).

4. Detailed Examples

4.1 Sequence Example – Computing the Area of a Circle

Given a radius \$r\$, compute the area \$A = \pi r^2\$.

READ r

SET A ← 3.14159 * r * r

WRITE "Area =", A

4.2 Decision Example – Grade Classification

Assign a grade based on a mark \$m\$ (0–100).

READ m

IF m >= 80 THEN

SET grade ← 'A'

ELSE IF m >= 70 THEN

SET grade ← 'B'

ELSE IF m >= 60 THEN

SET grade ← 'C'

ELSE IF m >= 50 THEN

SET grade ← 'D'

ELSE

SET grade ← 'F'

ENDIF

WRITE "Grade:", grade

4.3 Iteration Example – Sum of the First \$n\$ Natural Numbers

Calculate \$S = 1 + 2 + \dots + n\$.

READ n

SET i ← 1

SET S ← 0

WHILE i <= n DO

SET S ← S + i

SET i ← i + 1

ENDWHILE

WRITE "Sum =", S

4.4 Combined Example – Finding the Maximum in a List

Given \$n\$ integers stored in an array \$A[1..n]\$, determine the maximum value.

READ n

FOR i ← 1 TO n DO

READ A[i]

ENDFOR

SET max ← A[1]

FOR i ← 2 TO n DO

IF A[i] > max THEN

SET max ← A[i]

ENDIF

ENDFOR

WRITE "Maximum =", max

Suggested diagram: Flowchart showing the three constructs – a linear flow for sequence, a diamond for decision, and a loop back arrow for iteration.

5. Common Pitfalls

  • Forgetting to update the loop variable (causes infinite loops).
  • Using the wrong relational operator in a decision (e.g., = instead of ==).
  • Placing statements outside the intended block due to incorrect indentation.
  • Not handling edge cases such as \$n = 0\$ in loops.

6. Summary

  1. All algorithms can be expressed using sequence, decision, and iteration.
  2. Pseudocode provides a language‑independent way to describe algorithms.
  3. Clear structure, consistent indentation, and meaningful variable names improve readability.
  4. Testing with a range of inputs helps verify correctness and reveals hidden errors.

7. Practice Questions

#TaskKey Construct(s)
1Write pseudocode to compute the factorial of a positive integer \$n\$.Iteration (loop) and sequence
2Given three numbers, output them in ascending order.Decision (nested if) and sequence
3Read a list of \$m\$ integers and count how many are even.Iteration, decision, and sequence
4Design an algorithm that repeatedly asks the user for a password until it matches the stored value.Iteration (repeat‑until) and decision