Different people use different programming languages, and that often becomes a problem. If you implement an algorithm you've written in one particular language, developers who don't know that language would hardly be able to understand it. To solve this problem, we use pseudocode, a unique artificial language that stands somewhere between "human" language and code. Let's find out what it is and why we need it.
What is Pseudocode?
Despite the variety of programming languages, they all share some common features such as; variables, loops, if-else statements, and so on. In fact, if we remove all language-specific features from a program, we are left with its "logical core", which is the essence of any algorithm. By isolating this core, we are unavoidably forced to resort to high-level constructs like "Do A until B happens", where both A and B can be quite complex operations. So, this essence of an algorithm is called Pseudocode.
If we decide to use pseudocode, we lose the opportunity to "explain" our instructions to a computer, which requires a significantly lower-level input. On the other hand, we gain a lot from the conciseness of our solution and its comprehensibility to people, which is exactly what we strive for when we create Pseudocode.
Why do we need Pseudocode?
But why should we use an abstract language, not an existing one? Of course, we can use a popular and simple language like Python, and many programmers can understand this code. The main problem here is that in real code you need to work with memory, include libraries, and solve some problems with visibility, variable types, and so on. Why do we need to think about this stuff if we want to understand the algorithm? An abstract language better describes the idea of an algorithm without complications.
Another obvious solution to the problem of universal description of an algorithm is to simply describe it in human language. Alas, this is also a bad idea. In this case, you have to read a lot of text and take some time to figure out what the code will look like. With pseudocode, you don't need to clarify the description, and it's easy to see the structure of the code.
Let's solve a standard task and find the maximum value in an array of positive numbers. First, let's look at a pseudocode function:
function max(array): // you receive an array somehow if len(array) == 0 then // compare the size of array with 0 return -1 // empty array, no maximum max = 0 // assume that maximum is the 0 for i in [1, len(array)]: // iterate over the array, array indexing start with 1 in pseudocode if array[i] > max then // if we find something greater, we change the maximum max = array[i] return max // our result
It looks pretty straightforward and gives a sense of the algorithm's internal logic.
Now let's look at the Python code that does basically the same:
n = int(input()) # the size of array array =  # empty array for i in range(n): # do something n times array.append(int(input())) # add element to the array if n == 0: # empty array print(-1) else: max = 0 # current maximum for i in array: # iterate over the array if i > max: max = i # update the maximum print(max)
As you can see, we can skip reading and storing values. With Pseudocode, we can describe only the algorithm's logic.
As you already know, pseudocode is a way to show the structure of the algorithm without any pictures or explanations. Now, it is important to find out what our custom pseudocode looks like.
In this topic, we will agree upon several rules: how to assign a variable, how to print the output, and how to create a simple "if-else" statement.
1. Variables and assigning
Let's start learning our language with basic constructions such as variables and assigning. Look at the example below:
a = 3
Here, we create a variable named
a and assign an integer number
93 to it. The syntax is pretty simple: the name of the variable is on the left, the value is on the right, and the assignment operator is in between. Let's look at some other examples:
a = 5 b = 3.14 c = "Hello World!"
Here, we assign a floating-point number
3.14 to the variable
b and the string
Hello World! to the variable
s. For simplicity, we don't use types: we don't declare that the variable
s is a string or the variable
b is a floating-point number.
2. Input and Output Data
In our algorithms, we will typically avoid input and output. When you describe how an algorithm works, it is irrelevant how you get the data and what happens next. However, if we need some external data, we will use this:
a = input() // a is a number b = input() // b is a string
Note that we don't care about the input data type. Besides,
b can be not only numbers or strings. You can also read a table or a sequence of values. However, make sure to add a comment for the reader.
Some algorithms require sending data to the screen. We will use the following syntax for this task:
s = "Hello Reader" print(s)
3. Arithmetic Operations
Almost any program requires processing numerical data. For that, we use arithmetic operations. In our pseudocode, they look like this:
a = 10 b = 5 sum = a + b // addition, sum is equal to 15 sub = a - b // subtraction, equal to 5 product = 2 * 5 // multiplication division = a / b // division, equal to 2
4. Relational and Logical Operators
You can also use these relational operators in your pseudocode:
a == b // a equal to b a != b // a not equal to b a < b // a less than b a <= b // a less than or equal to b a > b // a greater than b a >= b // a greater than or equal to b
All these operations return a boolean value, i.e true or false.
You can also use logical operators. The
and returns true only if both conditions are true. The
or returns false only if both conditions are false. It works this way:
true and true == true true and false == false false and true == false false and false == false true or true == true true or false == true false or true == true false or false == false
5. Conditional Statements
Another commonly used type of construction is Conditional Statements. Let's have a look at an example:
a = 10 b = 5 if a < b then print("a is less than b") elif a > b then print("a is greater than b") else print("both numbers are equal")
Here, we create a variable
a and initialize it with the number
10. Then, we check if
a is less than
b and if it is true, we print it to the screen, otherwise we check if
a is greater than
b and if it is true, we print it to the screen, if none of the above statements are true, then we simply print both numbers are equal. The syntax is clear: the
if keyword is followed by a condition, and the next line gets executed only if the condition is true. If you need to combine several conditions, you can use and, or, and not operators as well.
Pseudocode is a way of expressing an algorithm without following fixed syntax rules. It is widely used to communicate the crux of an algorithm to others while ignoring the details of its implementation. With pseudocode, you can easily communicate ideas and concepts to other developers, no matter what language they write in.
If you learned something new from this blog post, feel free to comment about it, and if you have any feedback to share, I'd love to hear about it too!