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

One algorithm, three forms.

An algorithm is a sequence of defined steps that solves a problem. You can write it as structured English, draw it as a flowchart or write it as pseudocode, and the exam expects you to turn any one of these into either of the others.

The key idea

An algorithm is a solution to a problem expressed as a sequence of defined steps. Most algorithms take an input, process it and produce an output. Data is named with meaningful identifiers, listed in an identifier table with each one's data type and purpose. Every algorithm is built from three constructs: sequence, selection (IF, CASE) and iteration (FOR, REPEAT, WHILE). It can be documented as structured English, a flowchart or pseudocode.

START Total ← 0Count ← 0 INPUT Mark Mark = -1? Total ← Total + MarkCount ← Count + 1 INPUT Mark OUTPUTTotal / Count STOP YesNo
Fig. 1: a flowchart that inputs marks until the rogue value -1 and outputs their average
Section 01

Algorithms and identifiers.

Fig. 1 is an algorithm: a finite sequence of defined steps that always does the same thing for the same input. Before writing it, list the data it uses. Each item gets an identifier, and an identifier table records them.

IdentifierData typeDescription
MarkINTEGERA mark input by the user; -1 ends the input
TotalINTEGERRunning total of the marks input
CountINTEGERNumber of marks input (not counting -1)
Your turn
A program inputs the length and width of a rectangle in metres and outputs its area. Write an identifier table for it.
Examiner trap

An identifier table needs three things for each identifier: the name, its data type and what it is used for. Leaving out the description loses the mark.

Section 02

Sequence, selection, iteration.

Step through the programs in the lab and watch the trace table fill. The lab uses the IGCSE layout (THEN on its own line, WHILE ... DO), but the three constructs are the same; in the exam write the 9618 layout shown here.

DECLARE Day : INTEGER
DECLARE Count : INTEGER
DECLARE Temp : REAL
Count ← 0
FOR Day ← 1 TO 7
    INPUT Temp
    IF Temp > 30.0 THEN
        Count ← Count + 1
    ENDIF
NEXT Day
OUTPUT "Hot days: ", Count
ConstructMeaning9618 keywords
SequenceStatements run one after another, in orderEach line in turn
SelectionA condition decides which statements runIF ... THEN ... ELSE ... ENDIF, CASE OF ... OTHERWISE ... ENDCASE
IterationStatements are repeatedFOR ... NEXT (count-controlled), REPEAT ... UNTIL (post-condition), WHILE ... ENDWHILE (pre-condition)
Worked example Spot the constructs
Identify where each of the three constructs is used in the program above.
  1. Sequence: Count ← 0 runs first, then the loop, then the OUTPUT, in that order.
  2. Iteration: the FOR loop repeats its body 7 times, once per day.
  3. Selection: the IF statement adds 1 to Count only when the temperature is above 30.0.
Answer: Sequence: the order of the statements; iteration: FOR; selection: IF
Examiner trap

In 9618 pseudocode the THEN goes on the same line as the IF, and WHILE has no DO: WHILE Mark <> -1. Use <> for not equal, never != or =/=.

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

Which is the best definition of an algorithm?

Quick check+10 XP

Which three columns does an identifier table need?

Quick check+10 XP

In the lab, which construct makes a block of statements run more than once?

Quick check+10 XP

Which loop always runs its body at least once?

Section 03

Changing between forms.

Structured English uses short numbered English sentences with key words such as INPUT, SET, IF and REPEAT. A flowchart shows the same steps with standard symbols. Pseudocode is closest to program code. Here is Fig. 1 in structured English:

1. SET Total and Count to 0
2. INPUT a mark
3. WHILE the mark is not -1
       ADD the mark to Total
       ADD 1 to Count
       INPUT the next mark
4. OUTPUT Total divided by Count
Worked example Flowchart to pseudocode
Write pseudocode for the flowchart in Fig. 1.
  1. The two process boxes after START become Total ← 0 and Count ← 0; the first parallelogram becomes INPUT Mark.
  2. The decision is tested before the loop body, and the No path loops back to it, so it is a pre-condition loop: WHILE Mark <> -1. Note the condition is reversed: the loop continues while the answer is No.
  3. Inside the loop: Total ← Total + Mark, Count ← Count + 1, INPUT Mark, then ENDWHILE.
  4. The Yes exit leads to OUTPUT Total / Count. (If the first mark is -1, Count is 0 and the division fails; a better version checks Count > 0 first.)
Answer: Total ← 0, Count ← 0, INPUT Mark, WHILE Mark <> -1 ... ENDWHILE, OUTPUT Total / Count
Worked example Pseudocode to flowchart
Describe the flowchart for: REPEAT INPUT Guess UNTIL Guess = 7, followed by OUTPUT "Correct".
  1. START terminator, then an input/output parallelogram INPUT Guess.
  2. Then a decision diamond Guess = 7?. REPEAT is a post-condition loop, so the decision comes after the body.
  3. No: a flow line goes back up to the INPUT box. Yes: a flow line goes on to an output parallelogram OUTPUT "Correct", then a STOP terminator.
Answer: START, INPUT Guess, decision Guess = 7? (No loops back to INPUT), OUTPUT, STOP
Your turn
Write pseudocode from this structured English: 1. INPUT the price and the quantity. 2. SET the cost to price times quantity. 3. IF the cost is more than 100, take 10% off the cost. 4. OUTPUT the cost.
Examiner trap

In a flowchart, a loop is drawn with a decision and a flow line going back. Put the decision before the body for WHILE and after the body for REPEAT, and label both exits of every decision.

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 is the most suitable identifier for the number of seats left on a bus?

Exam style+20 XP

In a flowchart, a decision with a No path leading back to an earlier step, placed after the steps it repeats, represents which construct?

Exam style+20 XP

Which line is correct 9618 pseudocode?

Exam style+20 XP

The marks 12, 7, 20 and then -1 are input to the algorithm in Fig. 1. What is output?

Skill unlocked

Structured English, flowcharts and pseudocode, 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
Algorithms, identifier tables, the three constructs and converting between structured English, flowcharts and pseudocode.