What is pseudocode and why do we use it in programming?
As much as software developers would love to jump straight into programming when they get a new project or feature, that’s not a good idea and can lead to poor design and mistakes being made.
Firstly, the developer(s) will create algorithms to represent what they want the program to do, and you can see more on algorithms in our article “What is an algorithm and why do we use them in computing?”, then the algorithm is represented in pseudocode to check the design will work.
So, what is pseudocode?
Pseudocode is a methodology that allows the developer to represent the implementation of an algorithm. Pseudocode is not programming language specific, so it can be understood by programmers no matter their background or language expertise. It is a representation of code that can be understood by even a layman with very little programming knowledge.
Pseudocode is an implementation of an algorithm in the form of text and annotations written in plain English. It doesn’t have syntax like a programming language so isn’t something we compile or run on a computer.
The reason we use pseudocode, even though it can add more time on to the development process, is because it has some advantages for developers:
- It improves the readability of any approach and is one of the best ways to start implementation of an algorithm.
- It acts as a bridge between the program and the algorithm and flowchart. It works as rough documentation and so other developers can understand the program of another when the pseudocode has been written.
- The main goal is to explain exactly what each line of a program should do, making the actual code construction phase easier for the developer.
How do we write pseudocode?
Now, on to the important stuff! Firstly, we need to arrange the sequence of tasks we want our code to complete and start with a statement that establishes the main goal of the code, for example “This program will allow the user to check whether a number is even or odd”.
We also should indent our pseudocode in the same way that if else statements and loops are indented in programming code, this really helps the understanding of what the pseudocode is doing and improves the readability of it.
Let’s have a look at some examples of pseudocode. The first example we’re going to look at includes something called an “if…else” statement. This will tell our code that if a condition is met to do a particular thing, and if it isn’t to do something else:
If student’s grade is greater than or equal to 60
This easy algorithm will print whether a student has passed or failed a particular exam or assignment based on their score.
Let’s check out another example. This time, we’re going to include something called a while loop. This tells the algorithm to repeat a set number of tasks while a particular condition is met, normally a number-based condition. Every time we go round the while loop, we add or subtract one to or from the counter so that eventually we no longer the condition of the loop and exit it.
The example below will input student grades while a condition is met, then work out the class average:
Set total to zero
Set grade counter to one
While grade country is less than or equal to 10
Input the next grade
Add the grade onto the total
Add one to the grade counter
Set the class average to the total divided by 10
Print the class average
Good practice in writing pseudocode
It’s good practice to use appropriate naming convention as well, so everyone can understand what the pseudocode is doing. This includes using simple and distinct names to easily lay out what is happening, as well as using appropriate indentation and any spaces we need to.
Make sure that you include everything that’s going to happen in the actual code and include any standard programming structures such as ‘if-then’, ‘while’, ‘for’ and so on the way we would in programming to reflect more accurately what will happen. Saying that, our pseudocode shouldn’t be fully programmatic – don’t forget we want it to be simple enough for a layman or a client to understand.
Finally, let’s see an example of what a full program would look like when written in pseudocode. We’re going to take a program that will calculate the lowest common multiple for long input values. Bear in mind that different people will write pseudocode differently – in their own words so to speak. Yours might not look exactly like mine, but they both should follow the standards above and have the same rough processes.
Function lcm (Argument one, Argument two)
Calculate the lowest common variable of Argument 1 and Argument 2 by
dividing their product by their greatest common divisor product
return lowest common variable
Function greatestCommonDivisor (Argument one, Argument two)
If Argument two is equal to zero
Then return Argument one
Return the greatest common divisor
In the main function
Print prompt “input two numbers”
Take the first number from the user
Take the second number from the user
Send the first and second numbers to the lcm function and print the result to the user
Interested in computer engineering? Find out more about all the computer engineering courses we have available by clicking here.
Diploma in Computer Engineering
Diploma in Artificial Intelligence
Alternatively, you can view all our online engineering courses here.
How can buildings be classified?
How can buildings be classified? We spend a lot of time in various structures, more so than we ever do outside. There are different types of buildings we spend our time in and might be involved in constructing, so let’s have a look at how we can classify these buildings. Classification of buildings based on […]
What is Agile Software Development?
What is Agile Software Development? Agile development is an approach that is a combination of iterative and incremental sequences with a focus on process adaptability and customer satisfaction via a rapid delivery of the working product. Unlike the more traditional waterfall model previously used in development, where each step is completed sequentially, agile promotes an […]
What is the Software Development LifeCycle?
What is the Software Development LifeCycle? The SDLC (Software Development Life Cycle) is a breakdown of all the steps involved in the creation of software and will normally move through the following steps: Why is it used? The SDLC gives a business or team a tried-and-tested foundation for every software related project that they produce. […]