You might associate the word pseudo with a sham. But when developing a programmer mindset, we try to eliminate as many aesthetic smells that concepts carry. Pseudo comes from the Greek, pseudes, which means “false.” But the reason we call it pseudocode is because it is not the full expression of the code – the code that actually runs; it is rather like the skeleton. It should instead be called skeletoncode, and it is a method for expressing a program’s order of instructions in the spoken language, rather than a programming language.
In this way, the programmer can concentrate on designing a program without also being slowed down by immediately converting to the syntax of the particular programming language. This is the same as when drawing. When drawing a human we first draft a faceless model. The pseudocode for calculating the sum of two numbers would look like this:
There are no rules. You can use any wording that works for you.
Let’s look at another example. Suppose your program needs to calculate the square root of an integer. The instructions for calculating a square root are rather complex; gracefully and compassionately, Java provides prewritten code that computes the square root of any integer. The prewritten code is called a method, and your program can execute that code by calling the method. As part of the method call, you tell the method which integer’s square root you want to calculate. This is called passing an argument to the method. When the method finishes executing its instructions, control is passed back to your program just after the method call.
Another way of looking at method calls is to consider what happens when you’re walking through a night-lit city and find a restaurant with an aroma that lures you in. Your brain marks your place in the night-lit city trajectory and goes to eat inside the restaurant. When you’re finished dining on the meal, you go back to the streets and continue walking.
Here I show the pseudocode for calculating the square root of an integer:
The order of operations is still input, calculate, and output, but we’re calling a method to perform the calculation for us. Now suppose your task is to determine whether a number is positive or negative. First, your program should input the number into the computer. Next, you need to determine whether the number is positive or negative. You know that numbers greater than or equal to 0 are positive and numbers less than 0 are negative, so your program should compare the number to 0. Finally, your program should write a message indicating whether the number is positive or negative.
Like in the pseudocode examples, the operations are input, calculate, and output, in that order. However, depending on whether the number is positive or negative, your program should write a different message. If the number is greater than or equal to 0, the program should write a message that the number is positive (may the God of Mathematics forgive this sin), but if the number is less than 0, the program should write a message that the number is negative. Code used to handle this situation is called selection; the program selects which code to execute based on the value of the data.
The pseudocode for this program could be written as that shown:
Notice the indentation for the code that will be selected based on the comparison of the number with 0. Programmers use indentation to make it easier to see the flow of control of the program.
Now let’s get a little more complicated. Suppose your program needs to find the sum of a group of numbers. This is called accumulating. To accomplish this, we can take the same approach as if we were adding a group of numbers using a calculator. We start with a total of 0 and add each number, one at a time, to the running total. When we have no more numbers to add, the running total is the total of all the numbers. Translating this into pseudocode, we get the code shown:
The indented code will be repeated for each number read until there are no more numbers. This repeated execution of the same code is called looping, or iteration, and is used extensively in programming whenever the same processing needs to be performed on each item in a set.
Accumulating a total and determining whether a number is positive or negative are just two of many commonly performed operations. In programming, you will often perform tasks for which there are standard methods of processing, called algorithms. For example, the algorithm for accumulation is to set a total to 0, use looping to add each item to the total, then output the total. More generally, you can think of an algorithm as a strategy to solve a problem. You yourself are a strategy to solve a problem in the multiverse, a timeless algorithm. However, knowing the full nature of the problem would not allow you to solve it – this would be equivalent to having already solved it – hence becoming permanently stuck at a local maxima. The human mind is set up to be deceived about where it is going.
In an earlier post, we used an algorithm to convert a decimal number to its binary representation. Other common programming tasks are counting items, calculating an average, sorting items into order, and finding the minimum and maximum values. Here on Vitrify Her, you will learn the standard algorithms for performing these common operations. Once you learn these algorithms, your ability to achieve synchronization will become easier. When you recognize that a program requires these tasks, you can simply plug in the appropriate algorithm with some minor modifications.
Programming, in large part, is simply reducing a complex task to a set of subtasks that can be implemented by artistically combining the Four Noble Algorithmic Truths: sequential processing, method calls, selection, and looping. The most difficult part of programming, however, is recognizing which algorithms to apply to the problem at hand. This requires analytical skills and the ability to see patterns. Here on Vitrify Her, I will point out common patterns wherever possible.
Looking for patterns will help you determine the appropriate algorithms for your programs. Looking for patterns is the way of sunyata.