CIE AS & A Level Computer Science, section 9.2 (Algorithms)
Stepwise refinement
Stepwise refinement starts with a general, top-level description of the solution and repeatedly adds detail (top-down design) until each step is simple enough to be written as program code.
Worked example: a shop checkout
Level 1 (top level):
- Scan all items.
- Work out the total.
- Take payment.
Level 2 (refine step 1 and 2):
- Scan all items
- 1.1 Read an item code.
- 1.2 Find its price.
- 1.3 Add it to the list.
- 1.4 Repeat until there are no more items.
- Work out the total
- 2.1 Add all prices.
- 2.2 If the total is over 50, take 10% off.
Level 3 (pseudocode):
Total ← 0
INPUT Code
WHILE Code <> "END"
Total ← Total + FindPrice(Code)
INPUT Code
ENDWHILE
IF Total > 50 THEN
Total ← Total * 0.9
ENDIF
OUTPUT Total
Logic statements
A logic statement is a condition that is TRUE or FALSE. It is used in IF, WHILE and UNTIL. Conditions can be combined with AND, OR and NOT:
IF Age >= 13 AND Age <= 19 THEN
OUTPUT "Teenager"
ENDIF
| Expression | Meaning |
|---|---|
A AND B |
true only if both are true |
A OR B |
true if at least one is true |
NOT A |
true if A is false |
Using them
Choose conditions that cover all the cases, and test the boundaries (for example, what happens when Age is exactly 13).
Common mistake: stopping the refinement too early, so a step like "work out the total" has no detail. Keep refining until every step could be written as one or two program statements. Also, check conditions with AND and OR against the boundaries.
Exam tip: write the levels clearly, with each refinement numbered under its parent step. Use logic statements that match the wording ("between 13 and 19" needs AND).
Check yourself
What is stepwise refinement?
Starting with a general solution and adding more detail in stages until each step can be programmed.
Write a condition for "age is under 13 or over 65".
Age < 13 OR Age > 65.
What does `NOT` do to a condition?
It reverses it: true becomes false and false becomes true.