Pseudocode Complete Notes
Everything you need β data types, loops, arrays, functions, files, SQL and full worked examples. Built for Cambridge O-Level CS.
Every piece of data stored in a program has a type. Choosing the right type matters β it tells the computer how much memory to use and what operations are allowed.
Use DECLARE to tell the program a variable exists before using it.
DECLARE colour : STRING DECLARE age : INTEGER DECLARE price : REAL DECLARE flag : BOOLEAN DECLARE grade : CHAR
Use the arrow β to assign a value to a variable (read it as "gets" or "is set to").
colour β "red" age β 18 price β 9.99 flag β FALSE grade β 'A' CONST pi β 3.1415 // constant β cannot be changed later pi β 3.142 // β ERROR β cannot reassign a constant
OUTPUT "Enter your name" // shows a message on screen INPUT name // waits for user to type something OUTPUT "Hello, ", name // outputs text + variable together
| Operator | Meaning | Example | Result |
|---|---|---|---|
| + | Addition | 7 + 3 | 10 |
| - | Subtraction | 7 - 3 | 4 |
| * | Multiplication | 7 * 3 | 21 |
| / | Division (real result) | 7 / 2 | 3.5 |
| MOD | Remainder after division | 7 MOD 2 | 1 |
| DIV | Integer division (no decimal) | 7 DIV 2 | 3 |
| = | Equal to | x = 5 | TRUE/FALSE |
| <> | Not equal to | x <> 5 | TRUE/FALSE |
| > < >= <= | Comparison operators | age >= 18 | TRUE/FALSE |
| AND | Both conditions must be TRUE | x > 0 AND x < 10 | TRUE/FALSE |
| OR | At least one must be TRUE | x = 0 OR x = 1 | TRUE/FALSE |
| NOT | Inverts TRUE/FALSE | NOT flag | TRUE/FALSE |
| & | String concatenation | "Hi " & name | "Hi Ali" |
IF age >= 18 THEN OUTPUT "Eligible to vote" ELSE OUTPUT "Not eligible" ENDIF
IF hours > 50 THEN OUTPUT "Overtime Bonus" ELSE IF hours > 30 AND hours <= 50 THEN OUTPUT "Normal Salary" ELSE OUTPUT "Salary Deducted" ENDIF
OUTPUT "Enter temperature" INPUT temperature IF temperature > 40 THEN OUTPUT "Too Hot" ELSE OUTPUT "Pleasant weather" ENDIF
OUTPUT "Enter first number" INPUT num1 OUTPUT "Enter second number" INPUT num2 IF num1 > num2 THEN OUTPUT num1 * 3 ELSE IF num2 > num1 THEN OUTPUT num2 * 5 ELSE OUTPUT num1 + num2 ENDIF
Use CASE OF when you have one variable that can match many specific values. It is cleaner than writing lots of ELSE IF chains.
CASE OF variable value1 : OUTPUT "..." value2 : OUTPUT "..." value3 : OUTPUT "..." OTHERWISE : OUTPUT "Invalid input" ENDCASE
OUTPUT "Enter two numbers" INPUT num1, num2 OUTPUT "Enter operator (+, -, *, /)" INPUT operator CASE OF operator '+' : OUTPUT num1 + num2 '-' : OUTPUT num1 - num2 '*' : OUTPUT num1 * num2 '/' : OUTPUT num1 / num2 OTHERWISE : OUTPUT "Invalid operator" ENDCASE
OUTPUT "Enter grade (A/B/C/D/F)" INPUT grade CASE OF grade 'A' : OUTPUT "Grade A β 4.0 points" 'B' : OUTPUT "Grade B β 3.0 points" 'C' : OUTPUT "Grade C β 2.0 points" 'D' : OUTPUT "Grade D β 1.0 points" 'F' : OUTPUT "Grade F β 0.0 points" OTHERWISE : OUTPUT "Invalid grade" ENDCASE
FOR index β 1 TO 10 // code repeated 10 times NEXT index // Counting down using STEP FOR index β 10 TO 1 STEP -1 OUTPUT index NEXT index // Even numbers only using STEP 2 FOR index β 2 TO 20 STEP 2 OUTPUT index NEXT index
sum β 0 FOR index β 1 TO 20 OUTPUT "Enter a number" INPUT num sum β sum + num NEXT index OUTPUT "Sum: ", sum OUTPUT "Average: ", sum / 20
hotDays β 0 niceDays β 0 coldDays β 0 FOR index β 1 TO 7 OUTPUT "Enter temperature for day ", index INPUT temp IF temp > 30 THEN OUTPUT "Hot day" hotDays β hotDays + 1 ELSE IF temp >= 20 AND temp <= 30 THEN OUTPUT "Nice day" niceDays β niceDays + 1 ELSE OUTPUT "Cold day" coldDays β coldDays + 1 ENDIF NEXT index OUTPUT "Hot days: ", hotDays OUTPUT "Nice days: ", niceDays OUTPUT "Cold days: ", coldDays
index β 1 WHILE index <= 10 OUTPUT index index β index + 1 ENDWHILE
sum β 0 OUTPUT "Enter a number (0 to stop)" INPUT num WHILE num <> 0 sum β sum + num OUTPUT "Enter a number (0 to stop)" INPUT num ENDWHILE OUTPUT "Total: ", sum
flag β FALSE tries β 3 WHILE flag = FALSE AND tries > 0 OUTPUT "Attempts remaining: ", tries OUTPUT "Enter password" INPUT password IF password = "hello123" THEN flag β TRUE ENDIF tries β tries - 1 ENDWHILE IF flag = TRUE THEN OUTPUT "Access Granted" ELSE OUTPUT "Access Denied" ENDIF
OUTPUT "Enter a number" INPUT num index β 1 factorial β 1 WHILE index <= num factorial β factorial * index index β index + 1 ENDWHILE OUTPUT "Factorial: ", factorial
WHILE pre-condition
Checks condition BEFORE running. May never run if condition starts FALSE.
REPEAT UNTIL post-condition
Checks condition AFTER running. Always runs at least once. Loops UNTIL condition is TRUE.
// Keeps asking until user enters a number between 1 and 12 REPEAT OUTPUT "Enter month (1-12)" INPUT month UNTIL month >= 1 AND month <= 12
oddSum β 0 evenSum β 0 index β 1 REPEAT IF index MOD 2 = 0 THEN evenSum β evenSum + index ELSE oddSum β oddSum + index ENDIF index β index + 1 UNTIL index > 10 OUTPUT "Even sum: ", evenSum OUTPUT "Odd sum: ", oddSum
An array is a collection of values of the same data type, stored under one name and accessed by an index number.
// Declare β 10 integers, indexed 1 to 10 DECLARE Scores : ARRAY[1:10] OF INTEGER // Assign directly Scores[1] β 85 Scores[2] β 92 // Initialise all elements to 0 using a loop FOR index β 1 TO 10 Scores[index] β 0 NEXT index // Take input from user FOR index β 1 TO 10 OUTPUT "Enter score" INPUT Scores[index] NEXT index
FUNCTION LinearSearch(ValToFind : INTEGER) RETURNS BOOLEAN exit β FALSE index β 1 REPEAT IF ValToFind = Scores[index] THEN exit β TRUE ELSE index β index + 1 ENDIF UNTIL index > 10 OR exit = TRUE RETURN exit ENDFUNCTION
DECLARE Values : ARRAY[1:5] OF INTEGER FOR index β 1 TO 5 OUTPUT "Enter a value" INPUT Values[index] NEXT index largest β Values[1] // start with first element FOR index β 2 TO 5 IF Values[index] > largest THEN largest β Values[index] ENDIF NEXT index OUTPUT "Largest value: ", largest
// Sorts Scores[1:10] in ascending order upperBound β 10 REPEAT upperBound β upperBound - 1 swap β FALSE FOR index β 1 TO upperBound IF Scores[index] > Scores[index+1] THEN temp β Scores[index] Scores[index] β Scores[index+1] Scores[index+1] β temp swap β TRUE ENDIF NEXT index UNTIL upperBound = 0 OR swap = FALSE // stop early if already sorted
A 2D array is like a table with rows and columns. Think of it as a grid. You need two index numbers to access any one cell: Array[row, col].
// 5 students, 3 columns (e.g. score in 3 subjects) DECLARE Marks : ARRAY[1:5, 1:3] OF INTEGER Marks[1,1] β 85 // student 1, subject 1 Marks[2,3] β 70 // student 2, subject 3
// Taking input FOR rows β 1 TO 5 FOR cols β 1 TO 3 OUTPUT "Enter mark for student ", rows, " subject ", cols INPUT Marks[rows, cols] NEXT cols NEXT rows // Outputting values FOR rows β 1 TO 5 FOR cols β 1 TO 3 OUTPUT Marks[rows, cols] NEXT cols NEXT rows
OUTPUT "Enter value to find" INPUT ValToFind rows β 1 exit β FALSE REPEAT cols β 1 REPEAT IF Marks[rows, cols] = ValToFind THEN exit β TRUE ELSE cols β cols + 1 ENDIF UNTIL cols > 3 OR exit = TRUE rows β rows + 1 UNTIL rows > 5 OR exit = TRUE IF exit = TRUE THEN OUTPUT "Value found at row ", rows-1, " col ", cols ELSE OUTPUT "Value not found" ENDIF
OUTPUT "Enter the student ID" INPUT Id found β FALSE rows β 1 REPEAT IF StudentData[rows, 1] = Id THEN found β TRUE ELSE rows β rows + 1 ENDIF UNTIL rows > 1000 OR found = TRUE IF found = TRUE THEN OUTPUT "Name: ", StudentData[rows, 2] ELSE OUTPUT "ID not found" ENDIF
FUNCTION returns a value
Takes parameters, does something, and returns a result you can store in a variable.
PROCEDURE no return value
Takes parameters and does something (e.g. outputs to screen), but returns nothing.
FUNCTION GradeCalc(m1:INTEGER, m2:INTEGER, m3:INTEGER) RETURNS CHAR total β m1 + m2 + m3 percentage β total / 3 OUTPUT "Total: ", total, " Percentage: ", percentage CASE OF percentage >= 90 : grade β 'A' >= 80 : grade β 'B' >= 70 : grade β 'C' >= 60 : grade β 'D' OTHERWISE : grade β 'F' ENDCASE RETURN grade ENDFUNCTION // Calling the function OUTPUT "Enter marks for 3 subjects" INPUT s1, s2, s3 result β GradeCalc(s1, s2, s3) OUTPUT "Your grade is: ", result
PROCEDURE DrawTriangle(size : INTEGER) FOR rows β 1 TO size FOR col β 1 TO rows OUTPUT "*" NEXT col OUTPUT "" // new line NEXT rows ENDPROCEDURE // Calling the procedure DrawTriangle(5) // prints a 5-row triangle
Files let you store data permanently. Without files, all data is lost when the program ends. There are two modes: READ and WRITE.
OPENFILE "data.txt" FOR WRITE WRITEFILE "data.txt", "This is line one" WRITEFILE "data.txt", "This is line two" CLOSEFILE "data.txt"
OPENFILE "data.txt" FOR READ WHILE NOT EOF("data.txt") READFILE "data.txt", oneLine OUTPUT oneLine ENDWHILE CLOSEFILE "data.txt"
OPENFILE "exam_scores.txt" FOR READ OPENFILE "high.txt" FOR WRITE OPENFILE "medium.txt" FOR WRITE OPENFILE "low.txt" FOR WRITE WHILE NOT EOF("exam_scores.txt") READFILE "exam_scores.txt", score IF score >= 80 THEN WRITEFILE "high.txt", score ELSE IF score >= 50 THEN WRITEFILE "medium.txt", score ELSE WRITEFILE "low.txt", score ENDIF ENDWHILE CLOSEFILE "exam_scores.txt" CLOSEFILE "high.txt" CLOSEFILE "medium.txt" CLOSEFILE "low.txt"
SQL (Structured Query Language) is used to retrieve data from a database table. In your exam you need to know four keywords.
| Keyword | What it does | Required? |
|---|---|---|
| SELECT | Choose which columns (fields) to display. Use * to select all. | β Always |
| FROM | Which table to query. | β Always |
| WHERE | Filter rows based on a condition. | Optional |
| ORDER BY | Sort results. Add ASC (lowβhigh) or DESC (highβlow). | Optional |
-- Show all data SELECT * FROM STUDENT -- Show names of all Year 11 students scoring above 70, highest score first SELECT FirstName, LastName, Score FROM STUDENT WHERE YearGroup = 11 AND Score > 70 ORDER BY Score DESC -- Show all students in House 'Blue', sorted by name A-Z SELECT FirstName, LastName FROM STUDENT WHERE HouseColour = 'Blue' ORDER BY LastName ASC -- Show all Grade A students SELECT * FROM STUDENT WHERE Grade = 'A'
12 clubs. Track wins/draws/losses. Calculate points (Win=12, Draw=5, Loss=0). Find the club(s) with most points.
// Step 1: Input and validate number of matches (max 22) REPEAT OUTPUT "Enter number of matches played (max 22)" INPUT Matches UNTIL Matches >= 1 AND Matches <= 22 // Step 2: Input club names and match results FOR index β 1 TO 12 OUTPUT "Enter name of club ", index INPUT Clubs[index] // Validate: wins + draws + losses must equal Matches REPEAT OUTPUT "Enter matches won" INPUT won OUTPUT "Enter matches drawn" INPUT drawn OUTPUT "Enter matches lost" INPUT lost IF (won + drawn + lost) <> Matches THEN OUTPUT "Error: totals must add up to ", Matches ENDIF UNTIL (won + drawn + lost) = Matches Statistics[index, 1] β won Statistics[index, 2] β drawn Statistics[index, 3] β lost // Step 3: Calculate points Points[index] β (won * 12) + (drawn * 5) NEXT index // Step 4: Find the highest points total highest β -1 FOR index β 1 TO 12 IF Points[index] > highest THEN highest β Points[index] ENDIF NEXT index // Step 5: Find and output all clubs with the highest points DECLARE topTeams : ARRAY[1:12] OF INTEGER count β 0 FOR index β 1 TO 12 IF Points[index] = highest THEN count β count + 1 topTeams[count] β index ENDIF NEXT index FOR index β 1 TO count OUTPUT "Winner: ", Clubs[topTeams[index]] OUTPUT "Wins: ", Statistics[topTeams[index], 1] OUTPUT "Points: ", highest NEXT index
5Γ5 grid. Place 'X' randomly (not at [1,1]). Player starts at [1,1]. 10 moves to find X using W/A/S/D.
// Step 1: Initialise grid with empty strings FOR row β 1 TO 5 FOR col β 1 TO 5 Grid[row, col] β "" NEXT col NEXT row // Step 2: Place 'X' in a random cell (not [1,1]) REPEAT Xrow β ROUND(RANDOM() * 4, 0) + 1 Xcol β ROUND(RANDOM() * 4, 0) + 1 UNTIL Xrow <> 1 OR Xcol <> 1 Grid[Xrow, Xcol] β 'X' // Step 3: Initialise player position and move counter PlayerRow β 1 PlayerCol β 1 moveCount β 0 win β FALSE // Step 4: Game loop β up to 10 moves REPEAT OUTPUT "Position: [", PlayerRow, ",", PlayerCol, "] Moves left: ", 10 - moveCount OUTPUT "W=Up S=Down A=Left D=Right" INPUT move move β UCASE(move) error β FALSE tempR β PlayerRow tempC β PlayerCol CASE OF move 'W' : tempR β PlayerRow - 1 'S' : tempR β PlayerRow + 1 'A' : tempC β PlayerCol - 1 'D' : tempC β PlayerCol + 1 OTHERWISE : error β TRUE ENDCASE // Validate move is within grid bounds IF tempR < 1 OR tempR > 5 OR tempC < 1 OR tempC > 5 THEN error β TRUE OUTPUT "Move out of bounds β try again" ENDIF IF error = FALSE THEN PlayerRow β tempR PlayerCol β tempC moveCount β moveCount + 1 ENDIF // Check win condition IF Grid[PlayerRow, PlayerCol] = 'X' THEN win β TRUE OUTPUT "You Win! Found it in ", moveCount, " moves." ENDIF UNTIL win = TRUE OR moveCount = 10 IF win = FALSE THEN OUTPUT "You Lose! The X was at [", Xrow, ",", Xcol, "]" ENDIF
