Before beginning to code any algorithm, it is a good idea to plan what your algorithm is going to do and how you are going to do it.
One of the most common tools for designing algorithms is pseudocode, which is like real code but intended for human rather than machine reading.
In your exam, you might be asked questions (and asked to answer them) in pseudocode.
In this lesson, we’ll learn about:
Imagine you are writing a cooking recipe.
You have one part of your recipe that says, ‘Fry Beef’. You don’t say how to do it, or what to fry it with, you are leaving that up to the individual cook.
One cook might use a wok with some sesame oil, another a frying pan with vegetable oil. Both cooks will end up frying beef. How they do it is up to them.
This is the idea behind pseudocode. It’s a way of describing roughly what your program needs to do while leaving the actual code down to the specific language.
Consider creating a variable in C#, VB.NET and Python.
We will look at variables in more detail later on, but for now, just think of a variable as a bucket for storing data. In our case, we want to store a password.
Variables need names, so we will call this one “myPassword” and give it a value of “P@55worD”.
To write this in pseudocode, we might write something like this:
Create a variable "myPassword"
Set the value of "myPassword" to be "P@55worD"
This basically means, “create a variable called myPassword and put the data ‘P@55worD’ in it.”
To write this code in C# we would need to write:
string myPassword = "P@55worD";
To write this code in VB.NET we would need to write:
Dim myPassword As String = "P@55worD"
To write this code in Python we would need to write:
myPassword = "P@55worD"
You will see that although we are doing exactly the same thing, each language does it differently.
The pseudocode allows us to focus on WHAT the program is going to do rather than HOW we are going to actually code it.
We have seen pseudocode in the previous section. It is a form of ‘almost code’ that can vary greatly depending on who is writing it.
For example, the next slide contains three sets of pseudocode that are identical in function.
All three pieces of pseudocode do exactly the same thing:
However, they look quite different.
Example 1
print("please enter your name")
userInput = ReadInput()
print(userInput)
Example 2
userInput = Input("Please enter your name")
Output(userInput)
Example 3
print("please enter your name")
store input in variable userInput
output(userInput)
This is one of the issues with pseudocode, it can vary between schools and even between teachers.
This means you could be given an exam question in the form of pseudocode you are not familiar with.
To get around this problem, AQA has defined its own rules for pseudocode.
It looks very similar to normal pseudocode, but it is a lot more formal – in that it has absolutely defined ways of writing things that will not change between schools.
This way, everyone knows what to do.
In your exam, you will be expected to get the grammar and syntax of the AQA pseudocode as correct as possible.
We’ll use pseudocode throughout the programming lessons later in this course, but let’s review some of the basics now.
There is also a pseudocode cheat sheet in the media attachments in the introduction slide.
Let’s look at variables, input, output, and code comments in AQA pseudocode.
Let’s look at a simple program to add up two numbers and look at this program line by line.
Line 01
This is a comment. Comments don’t actually do anything and are simply used by the programmer to remind them what a block of code does.
Line 02
This prompts the user to enter a value.
Line 03
This declares a variable called number1 and stores the value input by the user.
Line 04
This prompts the user to enter a second value.
#a simple program to enter two numbers,
#add them and output the result to the user
OUTPUT 'Please enter the first number: '
number1 ← USERINPUT
OUTPUT 'Please enter the second number: '
number2 ← USERINPUT
result ← number1 + number2
OUTPUT 'The results is ' + result
Line 05
This declares a variable called number2 and stores the value input by the user.
Line 06
This declares a variable called result and then adds together the contents of the variables number1 and number2 and stores this in the result variable.
Line 07
This is where the result of the calculation is displayed to the user.
#a simple program to enter two numbers,
#add them and output the result to the user
OUTPUT 'Please enter the first number: '
number1 ← USERINPUT
OUTPUT 'Please enter the second number: '
number2 ← USERINPUT
result ← number1 + number2
OUTPUT 'The results is ' + result
Programmers use pseudocode to show how their code is going to work.
It is completely independent of any programming language.
AQA pseudocode is very similar to other forms of pseudocode.
It is more formal, though, in that it has its own rules.
You may be asked questions and asked to give answers in pseudocode.
It allows for code comments as well as variable declarations, input, and output.
This is a paper-based language only, and there is no compiler.