Paper 2: Pseudocode step by step (arrays, functions, procedures)
Pseudocode toolkit: the rules and the three loops
What you need to be able to do
- 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 up | How often | What it means for you |
|---|---|---|
| “Write pseudocode …” | 58 of 87 questions | Most marks come from writing code, not reading it. |
| A Module | Description table in the last question | 31 of 33 papers | You turn bullet points into a procedure or function (page 3). |
| A global array in the last question | 23 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 questions | Learn them before arrays: page 2. |
| Trace tables, flowcharts, structure charts, test data | 1 or 2 short questions per paper | Quick marks once you know the method (page 12). |
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 type | Holds | Example values |
|---|---|---|
| INTEGER | whole numbers | 0, 42, -7 |
| REAL | numbers with a decimal point | 3.5, 0.0, -2.25 (always write the point: 2.0) |
| CHAR | one character, in single quotes | 'A', '7', ' ' |
| STRING | any number of characters, in double quotes | "Ali", "" (the empty string) |
| BOOLEAN | TRUE or FALSE | TRUE, FALSE |
| DATE | a date | 02/11/2026 |
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| Operator | Meaning | Example | Result |
|---|---|---|---|
| + - * / | add, subtract, multiply, divide (/ always gives a REAL) | 7 / 2 | 3.5 |
| DIV | whole-number division (the quotient) | 7 DIV 2 | 3 |
| MOD | the remainder | 7 MOD 2 | 1 |
| ^ | power | 2 ^ 3 | 8 |
| = <> < > <= >= | compare (one = is a comparison, not assignment) | Age >= 18 | TRUE or FALSE |
| AND OR NOT | join conditions | Age >= 13 AND Age <= 19 | TRUE for a teenager |
| & | join (concatenate) strings | "Ali" & "Khan" | "AliKhan" |
Msg ← "Total: " & NUM_TO_STR(Total). In OUTPUT you can just use a comma instead: OUTPUT "Total: ", Total.Selection: IF and CASE
IF Mark >= 50 THEN OUTPUT "Pass"ELSE OUTPUT "Fail"ENDIFFor 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 Mark 80 TO 100 : Grade ← 'A' 60 TO 79 : Grade ← 'B' 40 TO 59 : Grade ← 'C' OTHERWISE : Grade ← 'U'ENDCASECASE 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
| Loop | Use it when | Runs 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 times | no: tested first | WHILE Found = FALSE … ENDWHILE |
| REPEAT (post-condition) | the body must run at least once (input, then check it) | yes: tested at the end | REPEAT … UNTIL Age > 0 |
FOR i ← 10 TO 1 STEP -1 // counting down: STEP -1 OUTPUT iNEXT iREPEAT OUTPUT "Enter a mark (0 to 100)" INPUT MarkUNTIL Mark >= 0 AND Mark <= 100UNTIL 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
Write pseudocode to input the marks of 10 students. Output how many students passed (a mark of 50 or more) and the average mark.
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
- 1count of passes
- 2average mark
- 10students: fixed number
- 50pass mark
- Counters and totals must start at 0 before the loop.
- The average is worked out after the loop, once.
- 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
- Work out firstPasses: +1 when Mark >= 50
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
- Declare the variables and set the counter and the total to 0.
- Loop 10 times: input a mark, add it to the total, and add 1 to Passes if it is 50 or more.
- After the loop, output Passes and Total / 10.
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 / 10Test 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"ENDIFMOD 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 iSTEP -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 PriceA 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"ENDCASEOne 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 CountWe 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 ", SumN 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 ", LowStart 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"ENDIFTwo 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
| Mistake | Write this instead |
|---|---|
Using = for assignment: Total = Total + Mark | Total ← Total + Mark |
| Starting a counter or total inside the loop | Set it to 0 once, before the loop |
| Missing ENDIF / NEXT / ENDWHILE | Write 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 round | UNTIL describes when to stop |
| Writing Python or Java | Paper 2 wants pseudocode: DECLARE, ←, ENDIF, NEXT … |
Can I do this?
- I declare variables with a data type and use ← for assignment.
- I know what DIV, MOD, / and & do.
- I can write nested IF and CASE with ranges and OTHERWISE.
- I choose FOR, WHILE or REPEAT and can say why.
- I set counters and totals before a loop and use a BOOLEAN flag to stop early.
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