Published by Patrick Mutisya · 14 days ago
To understand how to write clear pseudocode using the three basic programming constructs: sequence, decision, and iteration.
An algorithm is a finite, well‑defined set of instructions that solves a problem or performs a computation. It must be:
All algorithms can be built from three fundamental control structures. In pseudocode they are expressed as follows:
| Construct | Purpose | Pseudocode Example |
|---|---|---|
| Sequence | Execute statements one after another. |
|
| Decision (Selection) | Choose between alternative paths. |
|
| Iteration (Repetition) | Repeat a block of statements. |
|
IF, WHILE, ENDWHILE).READ and WRITE (or INPUT/OUTPUT).Given a radius \$r\$, compute the area \$A = \pi r^2\$.
READ r
SET A ← 3.14159 * r * r
WRITE "Area =", A
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
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
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
= instead of ==).| # | Task | Key Construct(s) |
|---|---|---|
| 1 | Write pseudocode to compute the factorial of a positive integer \$n\$. | Iteration (loop) and sequence |
| 2 | Given three numbers, output them in ascending order. | Decision (nested if) and sequence |
| 3 | Read a list of \$m\$ integers and count how many are even. | Iteration, decision, and sequence |
| 4 | Design an algorithm that repeatedly asks the user for a password until it matches the stored value. | Iteration (repeat‑until) and decision |