Paper 2: Pseudocode step by step (arrays, functions, procedures)

Paper 2: Pseudocode step by step (arrays, functions, procedures)

Pseudocode toolkit: the rules and the three loops

Pseudocode toolkit AS Level 15 min read

On this page
  1. What you need to be able to do
  2. What Paper 2 looks like
  3. Writing pseudocode the Cambridge way
  4. Selection: IF and CASE
  5. Loops: which one, and why
  6. Worked example: from words to pseudocode
  7. Practice: three levels
  8. Mistakes that lose marks
  9. Can I do this?

What you need to be able to do

Syllabus 9.2, 10.1, 11.1, 11.2 (Paper 2)
  • 11.1Declare variables and constants with the right data type, and use assignment, INPUT and OUTPUT.
  • 11.1Use the arithmetic, comparison, logic and string operators, including DIV, MOD and &.
  • 11.2Write selection with IF and CASE, and iteration with FOR, WHILE and REPEAT, choosing the right loop.
  • 9.2Write a short algorithm in pseudocode from a description in words.

This page is a warm-up: it collects the rules you already half-know into one place, because every later page (strings, functions, arrays) is built on them. If IF and loops already feel easy, skim it and do the L3 exercises.

What Paper 2 looks like

Paper 2 is 2 hours, 75 marks and half of your AS grade. It has about 6 to 8 questions, and they get longer as you go. We read every 9618 Paper 2 from 2021 to 2026 (33 papers) to see what they really ask:

What turns upHow oftenWhat it means for you
“Write pseudocode …”58 of 87 questionsMost marks come from writing code, not reading it.
A Module | Description table in the last question31 of 33 papersYou turn bullet points into a procedure or function (page 3).
A global array in the last question23 of 33 papers (13 use a 2D array)Arrays are the biggest single skill: pages 4 to 8.
String functions (MID, LENGTH, …)in about half the long questionsLearn them before arrays: page 2.
Trace tables, flowcharts, structure charts, test data1 or 2 short questions per paperQuick marks once you know the method (page 12).
The plan of this chapter: small steps. Each page teaches one idea, then gives exercises at three levels: L1 Basic (one idea at a time), L2 Medium (two ideas together) and L3 Hard (exam-sized). Only when the patterns are automatic do we move on to full past-paper questions.

Writing pseudocode the Cambridge way

Cambridge publishes its own pseudocode style. The examiner accepts small differences, but writing in this style means you never lose a mark for being unclear.

Data typeHoldsExample values
INTEGERwhole numbers0, 42, -7
REALnumbers with a decimal point3.5, 0.0, -2.25 (always write the point: 2.0)
CHARone character, in single quotes'A', '7', ' '
STRINGany number of characters, in double quotes"Ali", "" (the empty string)
BOOLEANTRUE or FALSETRUE, FALSE
DATEa date02/11/2026
Declaring, assigning, input and output
DECLARE Count : INTEGER          // a variable: declare it with its typeDECLARE Name, Town : STRING      // two variables of the same typeCONSTANT MaxMark = 100           // a constant: its value never changesCount ← 0                       // assignment: the value on the right goes into the box on the leftINPUT NameOUTPUT "Hello ", Name            // items separated by commas are output together
OperatorMeaningExampleResult
+ - * /add, subtract, multiply, divide (/ always gives a REAL)7 / 23.5
DIVwhole-number division (the quotient)7 DIV 23
MODthe remainder7 MOD 21
^power2 ^ 38
= <> < > <= >=compare (one = is a comparison, not assignment)Age >= 18TRUE or FALSE
AND OR NOTjoin conditionsAge >= 13 AND Age <= 19TRUE for a teenager
&join (concatenate) strings"Ali" & "Khan""AliKhan"
What do N MOD 2 = 0 and N MOD 10 give you? N MOD 2 = 0 is TRUE when N is even. N MOD 10 is the last digit of N (and N DIV 10 removes the last digit).
& joins strings only. To output “Total: 5” with a number in a STRING variable, convert first: Msg ← "Total: " & NUM_TO_STR(Total). In OUTPUT you can just use a comma instead: OUTPUT "Total: ", Total.

Selection: IF and CASE

IF … THEN … ELSE … ENDIF
IF Mark >= 50 THEN   OUTPUT "Pass"ELSE   OUTPUT "Fail"ENDIF

For many choices, use nested IF (an IF inside the ELSE) or a CASE statement. CASE is neater when you compare one variable with fixed values or ranges:

CASE OF … OTHERWISE … ENDCASE
CASE OF Mark   80 TO 100 : Grade ← 'A'   60 TO 79  : Grade ← 'B'   40 TO 59  : Grade ← 'C'   OTHERWISE : Grade ← 'U'ENDCASE

CASE tries the labels from the top and runs the first one that matches; OTHERWISE catches everything else. Ranges use TO, and single values are written on their own: 'X' : Result ← 1.

Loops: which one, and why

LoopUse it whenRuns at least once?Shape
FOR (count-controlled)you know how many times (10 marks, every element of an array)no (if the range is empty)FOR i ← 1 TO 10 … NEXT i
WHILE (pre-condition)you repeat while something is true, and it may be true zero timesno: tested firstWHILE Found = FALSE … ENDWHILE
REPEAT (post-condition)the body must run at least once (input, then check it)yes: tested at the endREPEAT … UNTIL Age > 0
FOR with STEP
FOR i ← 10 TO 1 STEP -1        // counting down: STEP -1   OUTPUT iNEXT i
Validation: keep asking until the input is valid
REPEAT   OUTPUT "Enter a mark (0 to 100)"   INPUT MarkUNTIL Mark >= 0 AND Mark <= 100
The loop condition is the opposite way round in the two conditional loops: WHILE runs while the condition is TRUE; REPEAT runs until the condition becomes TRUE. Writing UNTIL Mark < 0 OR Mark > 100 repeats only while the mark is valid, the wrong way round.

Worked example: from words to pseudocode

Example 1 · Ten marks (own example) ★ Easy
Q1Practice5 marks

Write pseudocode to input the marks of 10 students. Output how many students passed (a mark of 50 or more) and the average mark.

1 Read it with me what is actually happening?

We get ten marks, one at a time. For each mark we must remember two things: is it a pass, and how much does it add to the total?

  • input … 10 studentsa loop that runs exactly 10 times: FOR
  • how many passeda counter that goes up by 1
  • averagetotal ÷ how many
2 What is it asking for? the target, part by part
  • 1count of passesa counter
  • 2average marka total, divided by 10 at the end
3 What do we know? everything the question gives us
  • 10students: fixed number
  • 50pass mark
  • Counters and totals must start at 0 before the loop.
  • The average is worked out after the loop, once.
4 Plan the route work backwards from what we want
FindKey ideaAlready have itWork out firstTrap
  • Findpasses and average
    • Work out firstPasses: +1 when Mark >= 50
      • Trapset Passes to 0 before the loop, not inside it
    • Work out firstTotal: + Mark every time
    • Key ideaFOR Count ← 1 TO 10 around the INPUT
Which loop?

FOR: we know it is exactly 10 times.

Where does the average go?

After NEXT: dividing inside the loop would output 10 averages.

5 Now solve it have a go on paper first, then open the worked solution
  1. Declare the variables and set the counter and the total to 0.
  2. Loop 10 times: input a mark, add it to the total, and add 1 to Passes if it is 50 or more.
  3. After the loop, output Passes and Total / 10.
Model answer
DECLARE Mark, Count, Passes, Total : INTEGERPasses ← 0Total ← 0FOR Count ← 1 TO 10   INPUT Mark   Total ← Total + Mark   IF Mark >= 50 THEN      Passes ← Passes + 1   ENDIFNEXT CountOUTPUT "Passes: ", PassesOUTPUT "Average: ", Total / 10
6 Check it does the answer make sense?

Test it in your head with ten marks you can add easily (e.g. five 100s and five 0s: 5 passes, average 50). Our version was run with 50, 49, 100, 0, 75, 20, 60, 30, 90, 26 → Passes: 5, Average: 50.0.

Practice: three levels

L1 Basic

One idea at a time.

L1.1 Even or odd

Input a whole number and output Even or Odd.

Hint

What is the remainder when an even number is divided by 2?

Show the model answer
DECLARE N : INTEGERINPUT NIF N MOD 2 = 0 THEN   OUTPUT "Even"ELSE   OUTPUT "Odd"ENDIF

MOD 2 gives the remainder after dividing by 2: 0 for even numbers.

L1.2 Count down

Output the numbers 20, 18, 16, … down to 2.

Hint

Which STEP do you need?

Show the model answer
DECLARE i : INTEGERFOR i ← 20 TO 2 STEP -2   OUTPUT iNEXT i

STEP -2 counts down in twos.

L1.3 Ticket price

Input an age. Children under 12 pay 200, people aged 60 or more pay 250, everyone else pays 400. Output the price.

Show the model answer
DECLARE Age, Price : INTEGERINPUT AgeIF Age < 12 THEN   Price ← 200ELSE   IF Age >= 60 THEN      Price ← 250   ELSE      Price ← 400   ENDIFENDIFOUTPUT Price

A nested IF: the second test only happens when the first one is FALSE.

L2 Medium

Choose the right loop or selection.

L2.1 Valid score

Keep asking for a score until the user types a value from 0 to 10. Then output Thank you.

Show the model answer
DECLARE Score : INTEGERREPEAT   OUTPUT "Enter a score from 0 to 10"   INPUT ScoreUNTIL Score >= 0 AND Score <= 10OUTPUT "Thank you"

REPEAT, because the user must type at least once. The loop ends when the score is valid.

L2.2 Day name with CASE

Input a day number 1 to 7 and output its name (1 = Monday). Any other number outputs Invalid.

Show the model answer
DECLARE Day : INTEGERINPUT DayCASE OF Day   1 : OUTPUT "Monday"   2 : OUTPUT "Tuesday"   3 : OUTPUT "Wednesday"   4 : OUTPUT "Thursday"   5 : OUTPUT "Friday"   6 : OUTPUT "Saturday"   7 : OUTPUT "Sunday"   OTHERWISE : OUTPUT "Invalid"ENDCASE

One value per label, OTHERWISE for everything else. (Later you will store the names in an array instead.)

L2.3 Running total to a target

Input numbers and add them up until the total is more than 100. Output how many numbers were entered.

Show the model answer
DECLARE Total, Num, Count : INTEGERTotal ← 0Count ← 0WHILE Total <= 100   INPUT Num   Total ← Total + Num   Count ← Count + 1ENDWHILEOUTPUT Count

We don't know how many numbers there will be, so not FOR. WHILE tests the total before each new number.

L3 Hard

Two or three ideas together, like the short exam questions.

L3.1 Digits of a number

Input a positive whole number. Output how many digits it has and the sum of its digits (e.g. 4072 → 4 digits, sum 13). Use DIV and MOD, not string functions.

Hint

Write 4072 on paper and try MOD 10 and DIV 10 on it a few times.

Show the model answer
DECLARE N, Digits, Sum : INTEGERINPUT NDigits ← 0Sum ← 0REPEAT   Sum ← Sum + N MOD 10   N ← N DIV 10   Digits ← Digits + 1UNTIL N = 0OUTPUT Digits, " digits, sum ", Sum

N MOD 10 is the last digit; N DIV 10 removes it. REPEAT makes sure a one-digit number (even 0) counts as 1 digit.

L3.2 Highest and lowest

Input 8 temperatures (whole numbers, possibly negative). Output the highest and the lowest.

Hint

What goes wrong if you start High at 0 and every temperature is negative?

Show the model answer
DECLARE Temp, High, Low, i : INTEGERINPUT TempHigh ← TempLow ← TempFOR i ← 2 TO 8   INPUT Temp   IF Temp > High THEN      High ← Temp   ENDIF   IF Temp < Low THEN      Low ← Temp   ENDIFNEXT iOUTPUT "Highest ", High, " lowest ", Low

Start High and Low at the first value, not at 0: if every temperature is below zero, a starting High of 0 would be wrong. This idea comes back with arrays (pattern 3).

L3.3 Password attempts

The password is "Lahore26". Allow at most 3 attempts. Output Welcome as soon as the correct password is typed, or Locked after 3 wrong attempts.

Show the model answer
DECLARE Attempt : STRINGDECLARE Tries : INTEGERDECLARE Correct : BOOLEANTries ← 0Correct ← FALSEREPEAT   INPUT Attempt   Tries ← Tries + 1   IF Attempt = "Lahore26" THEN      Correct ← TRUE   ENDIFUNTIL Correct = TRUE OR Tries = 3IF Correct = TRUE THEN   OUTPUT "Welcome"ELSE   OUTPUT "Locked"ENDIF

Two reasons to stop, joined with OR. A BOOLEAN flag remembers which one happened. You will use this flag idea in every array search.

Mistakes that lose marks

MistakeWrite this instead
Using = for assignment: Total = Total + MarkTotal ← Total + Mark
Starting a counter or total inside the loopSet it to 0 once, before the loop
Missing ENDIF / NEXT / ENDWHILEWrite the end word as soon as you write the start word, then fill the middle
Joining a number with &: "Total " & Total"Total " & NUM_TO_STR(Total) (or a comma in OUTPUT)
UNTIL with the condition the wrong way roundUNTIL describes when to stop
Writing Python or JavaPaper 2 wants pseudocode: DECLARE, ←, ENDIF, NEXT …

Can I do this?

Anything unticked: revise that section first. All ticked: take the quiz.

The multiple-choice and guided questions need JavaScript. The concept checks and exam practice below work without it.

Concept checks

Answer each one in your head first, then open it.

1Why is REPEAT the natural loop for input validation?Show answer

The user must type something at least once before we can check it, and REPEAT tests at the end.

2Why start a maximum at the first value rather than 0?Show answer

0 might be bigger than every real value (e.g. negative temperatures), and then it would wrongly stay the maximum.

3What is a BOOLEAN flag for?Show answer

To remember that something happened (found, correct, swapped) so a loop or a later IF can use it.

Your progress on this note is saved when JavaScript is on.

⬇️ Download this chapter as a PDF: Full notes · Worksheets · Answers