AS Level · Topic 9.2
A-Level Computer Science 9618 / Topic 9 / AS

Add detail, one level at a time.

Nobody writes a complete program straight from a one-line problem statement. Stepwise refinement starts with a few big steps and keeps breaking each one into smaller, more detailed steps until every step can be programmed. Logic statements then pin down exactly when each part happens.

The key idea

Stepwise refinement is the process of breaking each step of an algorithm into smaller, more detailed steps, level by level, until every step is detailed enough to be written directly as program code. The levels are often numbered 1, 1.1, 1.1.1. Parts of the refined algorithm are defined with logic statements: conditions joined by AND, OR and NOT that evaluate to TRUE or FALSE, used in IF statements and loops. Use brackets to make the order of evaluation clear.

Car park charge 1 Get the inputs2 Work out charge3 Output charge 1.1 INPUT Minutes1.2 INPUT BlueBadge 2.1 hours, rounded up2.2 free if badge or2 hours or fewer 2.3 else 1.50 for eachhour after the first 22.4 maximum 12.00 3.1 OUTPUT a messageand the charge each level adds detail until every step can be written as one or two lines of code
Fig. 1: stepwise refinement of a car park charge algorithm to two levels of detail
Section 01

Top down, step by step.

A car park charges drivers when they leave. The first 2 hours are free; after that each hour or part of an hour costs $1.50, up to a maximum of $12.00. Blue badge holders park free. Fig. 1 shows how the problem is refined.

Your turn
Step 2.1 in Fig. 1 says "hours, rounded up". Is that detailed enough to program? If not, refine it.
Examiner trap

A refined step such as "work out the charge" earns no credit at the lowest level. Each final step must say exactly what is done, with the values or the calculation, so that it could be coded without further decisions.

Section 02

From outline to pseudocode.

When every step in Fig. 1 is refined fully, each one becomes a line or two of 9618 pseudocode. The step numbers show where each line came from.

CONSTANT FreeHours = 2
CONSTANT HourlyRate = 1.50
CONSTANT MaxCharge = 12.00
DECLARE Minutes : INTEGER
DECLARE Hours : INTEGER
DECLARE BlueBadge : BOOLEAN
DECLARE Charge : REAL

INPUT Minutes                               // step 1.1
INPUT BlueBadge                             // step 1.2
Hours ← (Minutes + 59) DIV 60              // step 2.1
IF BlueBadge = TRUE OR Hours <= FreeHours THEN   // step 2.2
    Charge ← 0.0
ELSE
    Charge ← (Hours - FreeHours) * HourlyRate   // step 2.3
    IF Charge > MaxCharge THEN                // step 2.4
        Charge ← MaxCharge
    ENDIF
ENDIF
OUTPUT "Charge: $", Charge                  // step 3.1
Worked example Test the refined algorithm
Work out the charge for (a) 130 minutes, no blue badge; (b) 1000 minutes, no blue badge; (c) 300 minutes with a blue badge.
  1. (a) Hours = (130 + 59) DIV 60 = 189 DIV 60 = 3. Not free, so Charge = (3 − 2) × 1.50 = 1.50.
  2. (b) Hours = 1059 DIV 60 = 17. Charge = 15 × 1.50 = 22.50, which is more than 12.00, so Charge = 12.00.
  3. (c) BlueBadge is TRUE, so the OR condition is TRUE and Charge = 0.0 without looking at the hours.
Answer: (a) $1.50 (b) $12.00 (c) $0.00
Your turn
What is the charge for 245 minutes with no blue badge?
Examiner trap

Adding 59 before DIV 60 rounds up to whole hours: 121 minutes gives 3 hours but 120 minutes gives 2. Writing Minutes DIV 60 would round down and undercharge.

Stage 1 · Learn

Check what the sim just showed you

Four quick checks. Each correct answer earns XP and records this skill on your profile.

Quick check+10 XP

What is stepwise refinement?

Quick check+10 XP

When should refinement of a step stop?

Quick check+10 XP

In the car park algorithm, what is the charge for 120 minutes without a blue badge?

Quick check+10 XP

In the lab's range check, which statement accepts an age from 11 to 18 inclusive?

Section 03

Logic statements.

Step 2.2 is a logic statement: "free if the driver has a blue badge OR has parked for 2 hours or fewer". In the lab, run the Range check program: the IF uses OR to spot an invalid age, and the UNTIL uses AND to accept a valid one.

Worked example Brackets change the meaning
A gym rule says: "A person may book a class if they are over 16 and have paid, or if they are a member of staff." Write a logic statement for CanBook, and show why the brackets matter for a 15-year-old member of staff who has not paid.
  1. Correct statement: CanBook ← (Age > 16 AND HasPaid) OR IsStaff.
  2. For the 15-year-old: (FALSE AND FALSE) OR TRUE = FALSE OR TRUE = TRUE, so they can book, as the rule says.
  3. Brackets in the wrong place: Age > 16 AND (HasPaid OR IsStaff) gives FALSE AND (FALSE OR TRUE) = FALSE AND TRUE = FALSE, which wrongly refuses them.
Answer: CanBook ← (Age > 16 AND HasPaid) OR IsStaff
Your turn
Write a logic statement that is TRUE when Mark is a valid percentage, a whole number from 0 to 100 inclusive.
Examiner trap

Each side of AND or OR must be a complete comparison. Mark >= 0 AND <= 100 is not valid; write Mark >= 0 AND Mark <= 100.

Stage 2 · Exam

Exam-style questions

Unlocks once the four checks above are done. Worth more XP, written to AS Paper 1 and Paper 2 standard.

Finish the four checks above to unlock the exam questions
Exam style+20 XP

Which logic statement is TRUE when Temp is outside the range 15 to 25 inclusive?

Exam style+20 XP

A = TRUE, B = FALSE, C = TRUE. What is the value of (A AND B) OR NOT C?

Exam style+20 XP

Which is the best refinement of the step "work out the area of a circle"?

Exam style+20 XP

Which statement about stepwise refinement is correct?

Skill unlocked

Stepwise refinement, mastered.

This skill is now recorded on your profile. Keep the chain going.

-Rank -Level -Score -Topics
Go deeper · practice
Six original Cambridge-style questions
Refining an algorithm level by level until it can be programmed, and writing logic statements with AND, OR and NOT.