Pseudocode 101.1 - Introduction to PseudocodeFitness Gear & Equipment
A short introduction to pseudocode.
After reading this article, you will be able to:
- Understand the advantages of using pseudocode over program flowcharts to write programs.
- Write a basic program using pseudocode.
Pseudocode is a method of displaying the logical flow and structure of a program. If structured correctly, the logic makes a program easier to follow and coding becomes a straightforward process.
There are two parts to a program - designing and writing the code. Once the logic of the design has been checked and corrected, the design is ready to be translated into code. Much time can be wasted in trying to find logic errors in a program that has been coded without a properly checked design. Just as a builder should never build a house without a properly drawn-up plan, one should never code a program without a proper design.
As a programmer, you might have learned about program flowcharts as a means of describing the flow of logic in a program.
Although a program flowchart is a useful tool for describing what a program must do, it has definite shortcomings:
- Program flowcharts are time consuming to construct.
- They can become clumsy and unmanageable when describing larger or more complicated programs.
- Flowcharts are often difficult to modify because they are, essentially, drawings.
- They are more difficult to read and follow, especially for larger programs.
Pseudocode offers a program design alternative to flowcharts. It is non-code-specific, easily readable, and can be translated into any high-level computer programming language.
Why use pseudocode?
Pseudocode is ideal for designing a program plan because it shows the step-by-step logic in a form that is easy to read and translate into a specific computer language. Pseudocode can easily be tested (using trace tables) to ensure that program output meets the requirements. Finding and correcting errors at this stage is far easier and less costly than after the coding has been completed.
You would use pseudocode instead of program flowcharts because it is:
- Less clumsy and less time consuming to prepare.
- Easier to modify, because there are no drawings.
- Easier to convert to a high-level programming language.
- Easier to read and understand.
- More powerful when dealing with larger, more complicated programs.
- Easily included in the final program in the form of comments.
There are literally thousands of programming languages worldwide that can be used to write a program. Some of them such as Python are used for designing graphical applications; others like C or C++ are used for engineering applications or in games development, while programs such as C#, Java and VB.Net are used in commercial and web development. Most of them use commands based on English.
Programming languages are described as either low level or high level. The closer the language is to machine code, the lower the level. Low-level languages use ones and zeroes, and communicate directly with the computer's processor, whereas, the high-level languages are more English-based and need to be converted to zeroes and ones that can be interpreted by the processor. The languages mentioned in the preceding paragraph are all examples of high-level languages.
Program flowcharts vs. pseudocode
For each process that can be described by a program flowchart symbol there is a corresponding pseudocode statement. Table 1 shows some of the more common symbols in program flowcharts, and their equivalent in pseudocode.
Table 1 - Program flowcharts and pseudocode compared
Simple pseudocode example
The following example shows how to code a simple program in pseudocode.
A program is required to calculate the annual cost of running a motor car. Costs include monthly instalments, annual fuel cost, and insurance. The annual insurance rate is 3% of the car's value.
Required input values are:
- Value of the motor car
- Monthly instalment amount
- Annual fuel cost
The output is the annual cost of running the car.
Answer - Pseudocode
input carValue, monthlyInstalment, annualFuelCost
annCost = monthlyInstalment * 12
insurance = carValue * 0.03 # or insurance = carValue * 3 / 100
totalCost = annCost + insurance + annualFuelCost
print "Total annual cost of running the car", totalCost