Pseudocode Examples
15 worked examples in Cambridge IGCSE and A-Level pseudocode, from a first OUTPUT to bubble sort. Each one shows its output, and you can run it in the editor below.
How to read these examples
Pseudocode describes the steps of a program in structured English, without the strict rules of a real programming language. Every example below uses the Cambridge style that IGCSE and A-Level exams use: DECLARE for variables, ← for assignment, OUTPUT to print, and blocks closed with ENDIF, NEXT or ENDWHILE.
Each example states a problem, gives the pseudocode, shows the output it produces and explains how it works. Press Run it in the editor to load any example into the editor at the top of the page, where you can step through it line by line and watch the variables change. New to the syntax? The pseudocode editor has a clickable library of every construct.
1. Hello, World
Print a message on the screen.
OUTPUT "Hello, World!"Hello, World!OUTPUT prints whatever follows it, and text goes in double quotes. This one line is a complete program: pseudocode needs no main function and no imports.
2. Add two numbers
Store two whole numbers, add them and print the total.
DECLARE a : INTEGER
DECLARE b : INTEGER
DECLARE total : INTEGER
a ← 12
b ← 30
total ← a + b
OUTPUT "The total is ", totalThe total is 42Each variable is declared with a name and a type before it is used. The arrow ← stores a value, so read total ← a + b as "total becomes a plus b". A comma in OUTPUT joins the text and the value on one line.
3. Even or odd
Decide whether a number is even or odd.
DECLARE n : INTEGER
n ← 17
IF n MOD 2 = 0 THEN
OUTPUT n, " is even"
ELSE
OUTPUT n, " is odd"
ENDIF17 is oddMOD gives the remainder after division. An even number leaves no remainder when divided by 2, so n MOD 2 = 0 is the test. IF picks one of two branches and ENDIF closes the block. In pseudocode a single = compares two values; it never assigns.
4. Grade a test score
Turn a score out of 100 into a grade: A for 80 and above, B for 60 to 79, C below 60.
DECLARE score : INTEGER
DECLARE grade : CHAR
score ← 74
IF score >= 80 THEN
grade ← 'A'
ELSE
IF score >= 60 THEN
grade ← 'B'
ELSE
grade ← 'C'
ENDIF
ENDIF
OUTPUT "Score ", score, " gets grade ", gradeScore 74 gets grade BThe checks run from the highest boundary down, so a score of 74 fails the first test and passes the second. Putting one IF inside the ELSE of another is how Cambridge pseudocode writes an else-if chain. A CHAR holds a single character in single quotes.
5. Sum the numbers from 1 to 10
Add up every whole number from 1 to 10.
DECLARE total : INTEGER
DECLARE i : INTEGER
total ← 0
FOR i ← 1 TO 10
total ← total + i
NEXT i
OUTPUT "Sum of 1 to 10 is ", totalSum of 1 to 10 is 55A FOR loop runs a fixed number of times: i takes the values 1, 2, 3 and so on up to 10, and NEXT i marks the end of the loop body. The running total starts at 0 and grows on every pass. The answer, 55, matches the formula n(n + 1) / 2.
6. Print a times table
Print the first five lines of the 7 times table.
DECLARE i : INTEGER
FOR i ← 1 TO 5
OUTPUT "7 x ", i, " = ", 7 * i
NEXT i7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35The loop counter i is used inside the calculation, so each pass prints a different line. OUTPUT can join several pieces of text and several values with commas.
7. Count down with a WHILE loop
Count down from 5 to 1, then print a final message.
DECLARE count : INTEGER
count ← 5
WHILE count > 0 DO
OUTPUT count
count ← count - 1
ENDWHILE
OUTPUT "Liftoff!"5
4
3
2
1
Liftoff!A WHILE loop checks its condition before every pass and stops as soon as the condition is false. Here the condition is count > 0, and the body lowers count by one each time. Leave out the line that lowers it and the loop never ends. Use WHILE when you don't know in advance how many passes you need.
8. Find the largest number in an array
Find the biggest value in a list of six numbers.
DECLARE nums : ARRAY[1:6] OF INTEGER
DECLARE i : INTEGER
DECLARE largest : INTEGER
nums[1] ← 14
nums[2] ← 3
nums[3] ← 27
nums[4] ← 9
nums[5] ← 21
nums[6] ← 6
largest ← nums[1]
FOR i ← 2 TO 6
IF nums[i] > largest THEN
largest ← nums[i]
ENDIF
NEXT i
OUTPUT "The largest number is ", largestThe largest number is 27ARRAY[1:6] declares six slots numbered 1 to 6, because Cambridge arrays usually start at 1. The program assumes the first element is the largest, then compares every other element against it and keeps whichever is bigger. After one pass through the array, largest holds the answer.
9. Calculate an average
Find the average of four test marks.
DECLARE marks : ARRAY[1:4] OF INTEGER
DECLARE i : INTEGER
DECLARE total : INTEGER
marks[1] ← 70
marks[2] ← 85
marks[3] ← 64
marks[4] ← 91
total ← 0
FOR i ← 1 TO 4
total ← total + marks[i]
NEXT i
OUTPUT "Average mark: ", total / 4Average mark: 77.5The loop adds every element of the array to total, then the program divides by the number of marks. The / operator gives a decimal result, which is why the output is 77.5. Use DIV instead when you want whole-number division.
10. Linear search
Find the position of the name Dara in a list of names.
DECLARE names : ARRAY[1:5] OF STRING
DECLARE i : INTEGER
DECLARE found : BOOLEAN
names[1] ← "Ana"
names[2] ← "Ben"
names[3] ← "Chen"
names[4] ← "Dara"
names[5] ← "Eli"
found ← FALSE
i ← 1
WHILE i <= 5 AND found = FALSE DO
IF names[i] = "Dara" THEN
found ← TRUE
ELSE
i ← i + 1
ENDIF
ENDWHILE
IF found = TRUE THEN
OUTPUT "Dara is at position ", i
ELSE
OUTPUT "Not found"
ENDIFDara is at position 4Linear search checks the elements one by one from the start. The WHILE condition stops the loop either when the name is found or when every element has been checked, and the flag found records which of the two happened. It works on unsorted data, but on a long list it can take one comparison per element.
11. Bubble sort
Sort five numbers into ascending order.
DECLARE nums : ARRAY[1:5] OF INTEGER
DECLARE i : INTEGER
DECLARE j : INTEGER
DECLARE temp : INTEGER
nums[1] ← 5
nums[2] ← 1
nums[3] ← 4
nums[4] ← 2
nums[5] ← 8
FOR i ← 1 TO 4
FOR j ← 1 TO 5 - i
IF nums[j] > nums[j + 1] THEN
temp ← nums[j]
nums[j] ← nums[j + 1]
nums[j + 1] ← temp
ENDIF
NEXT j
NEXT i
FOR i ← 1 TO 5
OUTPUT nums[i]
NEXT i1
2
4
5
8Bubble sort compares each pair of neighbours and swaps them when they are in the wrong order. After each pass of the outer loop, the largest remaining value has moved to the end, so the inner loop can stop one element earlier every time. The swap needs a third variable, temp, to hold one value while the other is copied over.
12. Factorial with a function
Write a function that returns n factorial (n!), then use it to work out 5!.
FUNCTION Factorial(n : INTEGER) RETURNS INTEGER
DECLARE result : INTEGER
DECLARE i : INTEGER
result ← 1
FOR i ← 2 TO n
result ← result * i
NEXT i
RETURN result
ENDFUNCTION
OUTPUT "5! = ", Factorial(5)5! = 120A FUNCTION takes parameters, does its work and hands a value back with RETURN; its header states the type it returns. Variables declared inside it exist only while it runs. 5! is 5 × 4 × 3 × 2 × 1, which is 120.
13. Count the vowels in a word
Count how many vowels the word PSEUDOCODE contains.
DECLARE word : STRING
DECLARE letter : CHAR
DECLARE vowels : INTEGER
DECLARE i : INTEGER
word ← "PSEUDOCODE"
vowels ← 0
FOR i ← 1 TO LENGTH(word)
letter ← SUBSTRING(word, i, 1)
IF letter = 'A' OR letter = 'E' OR letter = 'I' OR letter = 'O' OR letter = 'U' THEN
vowels ← vowels + 1
ENDIF
NEXT i
OUTPUT word, " has ", vowels, " vowels"PSEUDOCODE has 5 vowelsLENGTH gives the number of characters in a string, and SUBSTRING(word, i, 1) takes one character starting at position i. The loop visits every character and adds one to the counter whenever it is A, E, I, O or U.
14. FizzBuzz
Print the numbers 1 to 15, but print Fizz for multiples of 3, Buzz for multiples of 5 and FizzBuzz for multiples of both.
DECLARE i : INTEGER
FOR i ← 1 TO 15
IF i MOD 15 = 0 THEN
OUTPUT "FizzBuzz"
ELSE
IF i MOD 3 = 0 THEN
OUTPUT "Fizz"
ELSE
IF i MOD 5 = 0 THEN
OUTPUT "Buzz"
ELSE
OUTPUT i
ENDIF
ENDIF
ENDIF
NEXT i1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzzThe order of the checks matters. 15 is a multiple of 3 and of 5, so the test for 15 has to come first; otherwise the program would print Fizz and never reach FizzBuzz. It is a classic interview warm-up because it tests exactly this kind of reasoning.
15. Password attempts with REPEAT UNTIL
Try up to three passwords in turn and grant access if one of them is correct.
DECLARE attempts : ARRAY[1:3] OF STRING
DECLARE tries : INTEGER
DECLARE guess : STRING
attempts[1] ← "hunter1"
attempts[2] ← "letmein"
attempts[3] ← "open sesame"
tries ← 0
REPEAT
tries ← tries + 1
guess ← attempts[tries]
OUTPUT "Try ", tries, ": ", guess
UNTIL guess = "open sesame" OR tries = 3
IF guess = "open sesame" THEN
OUTPUT "Access granted"
ELSE
OUTPUT "Locked out"
ENDIFTry 1: hunter1
Try 2: letmein
Try 3: open sesame
Access grantedA REPEAT ... UNTIL loop checks its condition at the end, so the body always runs at least once. It stops when the password matches or after the third try. The guesses come from an array so the example runs without typing; in an exam answer you would usually read each one with INPUT.
Pseudocode examples FAQ
What is an example of pseudocode?
A short one decides whether a test is passed: IF score >= 50 THEN OUTPUT "Pass" ELSE OUTPUT "Fail" ENDIF. It reads almost like English but follows fixed rules, so it can be turned into Python, Java or any other language line by line. The 15 programs on this page go from a single line up to searching and sorting.
How do you write pseudocode?
Decide what the program must do, then write the steps in order with a small set of keywords: DECLARE for variables, ← to assign, IF for decisions, FOR or WHILE for repetition and OUTPUT for results. Indent the body of every block, close it with its end keyword, and keep one action per line.
Is pseudocode the same in every exam?
No. Cambridge (IGCSE and A-Level) uses DECLARE and the ← arrow, AQA uses ← without declarations, OCR's Exam Reference Language looks like Python, and Edexcel writes SET ... TO. The examples here use Cambridge; the pseudocode editor switches between all four styles.
Can I run pseudocode?
Yes, on this page. Pseudocode is not a real programming language, so there is no official compiler, but the editor here runs Cambridge pseudocode in your browser, shows the output and steps through it line by line. Press Run it in the editor under any example.
What is the difference between pseudocode and a flowchart?
Both describe an algorithm before it is coded. A flowchart draws the steps as boxes and arrows, which suits short processes with a few decisions. Pseudocode writes them as structured text, which scales to longer programs and turns into real code more directly.
Do I have to declare variables in pseudocode?
In Cambridge pseudocode, yes: mark schemes expect DECLARE name : TYPE before a variable is used. Other styles, such as AQA's, skip declarations. Declaring a variable also tells the reader what type of value it holds.