This section aims to provide a quick reference guide for Pseudocode at GCSE level and at IB / A-Level. Please note that the exact structure expected by the awarding body may change.
WHAT IS PSEUDOCODE
Pseudocode bridges the gap between plain English and programming languages. Pseudocode is used to identify the flow of a program and can be used to represent any programming language. Using pseudocode to plan and debut programs is common practice.
Below is an example of a task written in plain English: If the value of X is 10 and the value of Y is 10. If the value of Z is X + Y then the numeric answer to what is the value of Z is: 20.
The same scenario written in pseudocode: X ← 10 Y ← 10 Z ← X + Y print Z Output: 20
Above shows that pseudocode is just a small step from plain English to a programming language. The code above stores(assigns) the value of 10 in X, stores the value of 10 in Y, stores the value of X + Y in Z and then print the answer contained in Z.
In coding any the X,Y and Z would but called variables. A variable is like a box, we can put things in, take things out and change what is in them. In programming terms it is called assigning a value to the variable.
Decomposition Decomposition is a process of breaking problems down into smaller elements.
Supposing we want to create a program that works out the average test score from a class of 10 students, we need to ask the students their results, and then print the average result. We can break this problem down in to its smaller elements and list each stage in structured English.
STRUCTURED ENGLISH STEP1: Ask each of the 10 student their result STEP 2: Note down each of the 10 results STEP 3: Add up each result after noting down STEP 4: After asking all 10 students note down the total from part 3 STEP 5: Divide the total by 10 STEP 6: Tell the students the average results
Now we have broken the task down in to small manageable elements it is now reasonably easy to write the pseudocode for this task. We can see that tasks 1,2 and 3 are repeating 10 times, once for each student in the class. To repeat this process, we can use a for loop. A for loop in coding will automatically add(iterate) the count value by 1 each time it goes through the loop.
PSEUDOCODE FOR count ← 0 TO 10: OUTPUT "Please enter a student result" studentResult ← USERINPUT total ← total + studentResult NEXT count total ← total/10 PRINT total
As can be seen the pseudocode closely follows the bullets list of instructions. It repeats the first 3 tasks 10 times and each time it adds to the total value. Code that is run inside the FOR loop is indent (moved/tabbed in from the left margin), this is to highlight that it is code to be run as part of the loop.
While Loop The while loop will continue to loop while a condition is met. We naturally use while loops in our heads, often without realising, for example While it is raining outside, stay indoors, else; go out to play.
PLAIN ENGLISH: Guessing game with a while loop Select a random number between 1 and 10, ask your friend to guess that number, if they get the number correct then say "Well Done" if they do not get the number correct then ask them to keep guessing.
STRUCTURED ENGLISH: Guessing game with a while loop STEP 1: Generate a random number between 1 and 10 STEP 2: Ask your friend to guess the number STEP 3: Check if the number matches the generated number in step 1 STEP 4: If the number guessed does match the generated number then say "well done" and end game STEP 5: If the number does not match the generated number then repeat process from step 2
PSEUDOCODE: Guessing game with a while loop number ← generate random number between 1 and 10 correct ← FALSE WHILE correct = FALSE OUTPUT "Please guess a number between 1 and 10" guess ← USERINPUT IF guess = number: OUTPUT "Well done, you guess correct" correct ← TRUE ELSE: PRINT "Your guess was incorrect" correct ← FALSE
Notice that the random number is generated outside of the while loop, this means the number will remain the same for each game until the user has guessed the correct number. Also, the variable needed to be set to false before the while loop could compare it to a value.