/    /  DAA- Pseudocode for expressing algorithms

Pseudocode for expressing algorithms

 

What is a pseudocode?

Pseudocode, as the name suggests, is a false code or is a high-level description of an algorithm. In other words, we can say that pseudocode is a cooked-up representation of an algorithm. Pseudocode can be easily understood by someone who has basic knowledge of programming.

 

Pseudocode does not have a specific syntax unlike a program that is written using syntaxes of a particular language. Hence, pseudocode cannot be executed on a computer, rather it eases the work of a programmer as it can be easily understood.

 

While writing pseudocode one can use control structures that include for, while, if-then-else, repeat until. These control structures are common in almost all programming languages and hence can be used when required. Once we are done with the pseudocode we can easily modify it into a program in our preferred language.

 

Another advantage of pseudocode is that the running time of the program can be estimated in a general manner using it. A pseudocode gives us a count of fundamental operations. Also, pseudocode avoids any ambiguity that might be present in plain text.

 

Difference between Algorithm and Pseudocode:

An algorithm is a well defined sequence of instructions that provide a solution to the given problem.

A pseudocode is a method which is used to represent an algorithm.

 

An algorithm has some specific characteristics that describe the process.

A pseudocode on the other hand is not restricted to something. It’s only objective is to represent an algorithm in a realistic manner.

 

An algorithm is written in plain English or general language.

A pseudocode is written with a hint of programming concepts such as control structures.

 

To understand the difference between an algorithm and pseudocode lets have a look into the following example:

 

Consider an example of calculating the area of a circle.

 

Algorithm:
  1. Start.
  2. Read r: radius value as the input given by the user.
  3. Calculate the Area: 3.14 * r * r.
  4. Display the Area.
  5. End.

 

Pseudocode:
AreaofCircle() 
{ 
BEGIN 
Read: Number radius, Area; 
Input r; 
Area = 3.14 * r * r; 
Output Area; 
END 
}

 

Advantages of Pseudocode:

  • Pseudocode improves the readability and is the best way to start implementing an algorithm. 
  • A pseudocode works as a rough documentation. It makes the work of a programmer easy as it helps them understand the code better.
  • It acts as a bridge between a program and an algorithm.
  • It makes the process of constructing a code easy.

 

How to write a pseudocode?

As mentioned, a pseudocode is a method used to implement an algorithm. However there are a few points to be noted while writing a pseudocode. They are:

 

  1. First, arrange the tasks in a sequence so that the pseudo code can be written by following the same sequence. This will make the process more clear and simple.

 

  1. A pseudocode is something that can be understood by any basic programmer. However, in case of complex problems it could be difficult to understand the main goal of the problem if it is not specified in the pseudocode. So, do include a statement that established the main goal.

 

  1. Follow the indentation and whitespaces as you would do while writing an actual program. Indentation helps greatly with readability.

Example 1

if  “1” printresponse

“I am case 1”

 

Example 2

if  “2”

print response

“I am case 2”

 

Here, both the examples have the same meaning. However, the 2nd example is more clear and understandable compared to the 1st. This is the magic of Indentation and white spaces.

 

  1. Following a naming convention is important. Sometimes, naming in the wrong way can lead to confusion. So, naming must be done in a simple and distinct way.

 

  1. Sentence casings will always help you differentiate between constants, variables, methods, and others. This will avoid any confusion that might be present in the algorithm.

For methods use CamelCase

For constants use UPPERCASE

For variables use lowercase

 

  1. A pseudocode is supposed to explain the code in detail. Do not keep the pseudocode abstract.

 

  1. Use of control structures such as ‘for’, ‘while’, ‘if-then’ and ‘cases’ makes it easy while you develop a program using the pseudocode as reference.

 

  1. A pseudocode must be perfect in order for the code to be perfect. So do check if the pseudo code consists of any infinite loops or any gaps.

 

  1. A pseudo code must be must enough to be understood by a layman so do not write it in a programmatic way.

 

Do’s and Don’ts while writing a Pseudocode:

Do’s:
  • Make use of control structures.
  • Naming conventions must be followed properly.
  • Use indentation and white spaces wherever required as these are the key points for a pseudocode.
  • Keep the pseudocode simple and concise.

 

Don’ts:
  • Do not generalize the pseudocode.
  • A pseudocode mustn’t be abstract.

 

Make sure to consider all these points the next time you write a pseudocode. This would make it easy for you while you write the code later. 

 

Read the next article to understand the concepts of space complexity and time complexity.