Before beginning to code any algorithm, it is a really 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 algorithm is the use of “Pseudocode” which is like real code but is intended for human reading rather than machine reading. In your exam, you might be asked questions (and asked to answer) 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 is basically saying “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.
This is where Pseudocode is incredibly important because it means we can focus on WHAT the program is going to do, rather than HOW we are going to actually code it.
Look at the Pseudocode in the above example. For each language which part of the code involves creating the variables? Why does C# and VB.NET mention the word String and Python does not?
We have seen pseudocode above. It is a form of ‘almost code’ that can vary a lot depending on who is writing it. For example, the following three sets of pseudocode are identical in that they allow a user to enter a word, store it in a variable and then output the result.
print("please enter your name") userInput = ReadInput() print(userInput)
userInput = Input("Please enter your name") Output(userInput)
print("please enter your name") store input in variable userInput output(userInput)
All three pieces of pseudocode do exactly the same thing: they declare a variable called userInput, store whatever a user types into the console into it, and then output the result.
However, they look quite different.
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 a form of pseudocode you are not familiar with.
To get around this problem, AQA have defined their 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.
Look at the pseudocode in the above example. Why do you think you can have so many different ways of writing pseudocode, but only one way of writing with AQA pseudocode?
We’ll be using pseudocode throughout the programming lessons later in this course, but let’s have a look at some of the basics now. There is also a pseudocode cheat sheet in the media attachments at the top of this page.
Let’s look at variables, input, output and code comments in pseudocode.
Variables are created by simply stating their name.
Assigning a value to a variable (storing a piece of data in the variable) uses the
Getting input from a user and storing it in a variable uses the
Printing out to the console (displaying output to the user) uses the
Comments (little notes we add to explain our code) are written using
Let’s look at a simple program to add up two numbers and look at this program line by line.
#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
Are any of the code command words used above similar to any other languages? Is pseudocode very different from Python?
So to summarise what we’ve learnt in this lesson: