CIE AS & A Level Computer Science, section 9.2 (Algorithms)
Pseudocode
Pseudocode is a way of writing an algorithm in structured, language-neutral statements that is close to a program but is not tied to one programming language. In the CIE pseudocode:
- the assignment arrow is
←, INPUTreads from the keyboard andOUTPUTwrites to the screen,- keywords are written in capitals, and statements inside a construct are indented.
Input, process, output
DECLARE Name : STRING
DECLARE Age : INTEGER
INPUT Name
INPUT Age
OUTPUT "Hello ", Name
OUTPUT "Next year you will be ", Age + 1
The three basic constructs
1. Sequence: statements run one after another, in order.
2. Selection: a decision chooses which statements to run.
IF Mark >= 50 THEN
OUTPUT "Pass"
ELSE
OUTPUT "Fail"
ENDIF
3. Iteration (repetition): statements are repeated.
FOR Count ← 1 TO 5
OUTPUT Count
NEXT Count
Worked example
Read marks until -1 is entered, and output the number of marks and the total.
Total ← 0
Count ← 0
INPUT Mark
WHILE Mark <> -1
Total ← Total + Mark
Count ← Count + 1
INPUT Mark
ENDWHILE
OUTPUT Count, Total
total = count = 0
mark = int(input())
while mark != -1:
total += mark
count += 1
mark = int(input())
print(count, total)
Writing good pseudocode
- Use meaningful identifiers.
- Indent statements inside constructs.
- Make sure each construct is closed (
ENDIF,ENDWHILE,NEXT).
Common mistake: using = for assignment in pseudocode. Assignment is ←; = is used to compare in a condition. Also, forgetting to close a construct with ENDIF, ENDWHILE or NEXT loses marks.
Exam tip: write pseudocode that follows the CIE style: capital keywords, ← for assignment, indentation inside constructs. Check by dry running with a small test value.
Check yourself
Identify the three basic constructs.
Sequence, selection and iteration.
Which symbol is used for assignment in the pseudocode?
The left arrow, ←.
What does the statement `OUTPUT` do?
It displays data on the screen.