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.
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.
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.
| Identifier | Data type | Description |
|---|---|---|
| Mark | INTEGER | A mark input by the user; -1 ends the input |
| Total | INTEGER | Running total of the marks input |
| Count | INTEGER | Number of marks input (not counting -1) |
Total, not T or X1), start with a letter, contain only letters, digits and the underscore, and are never keywords such as OUTPUT or WHILE.NumberOfPlayers, and treats them as case insensitive.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.
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
| Construct | Meaning | 9618 keywords |
|---|---|---|
| Sequence | Statements run one after another, in order | Each line in turn |
| Selection | A condition decides which statements run | IF ... THEN ... ELSE ... ENDIF, CASE OF ... OTHERWISE ... ENDCASE |
| Iteration | Statements are repeated | FOR ... NEXT (count-controlled), REPEAT ... UNTIL (post-condition), WHILE ... ENDWHILE (pre-condition) |
Count ← 0 runs first, then the loop, then the OUTPUT, in that order.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 =/=.
Four quick checks. Each correct answer earns XP and records this skill on your profile.
Which is the best definition of an algorithm?
Which three columns does an identifier table need?
In the lab, which construct makes a block of statements run more than once?
Which loop always runs its body at least once?
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
Total ← 0 and Count ← 0; the first parallelogram becomes INPUT Mark.WHILE Mark <> -1. Note the condition is reversed: the loop continues while the answer is No.Total ← Total + Mark, Count ← Count + 1, INPUT Mark, then ENDWHILE.OUTPUT Total / Count. (If the first mark is -1, Count is 0 and the division fails; a better version checks Count > 0 first.)REPEAT INPUT Guess UNTIL Guess = 7, followed by OUTPUT "Correct".INPUT Guess.Guess = 7?. REPEAT is a post-condition loop, so the decision comes after the body.OUTPUT "Correct", then a STOP terminator.INPUT Price, INPUT Quantity, Cost ← Price * Quantity, IF Cost > 100 THEN Cost ← Cost * 0.9 ENDIF, OUTPUT Cost (with Price and Cost declared as REAL, Quantity as INTEGER).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.
Unlocks once the four checks above are done. Worth more XP, written to AS Paper 1 and Paper 2 standard.
Which is the most suitable identifier for the number of seats left on a bus?
In a flowchart, a decision with a No path leading back to an earlier step, placed after the steps it repeats, represents which construct?
Which line is correct 9618 pseudocode?
The marks 12, 7, 20 and then -1 are input to the algorithm in Fig. 1. What is output?
This skill is now recorded on your profile. Keep the chain going.