How to Write Pseudocode?

By | May 28, 2020
How to Write Pseudocode?

In this article, we will discuss the most common terminology we used in computer science called pseudocode, and learn how can we write algorithms using it. But before jumping to space where we will learn how we can write pseudocode, let’s first discuss what pseudocode is? And why do we need it?

What is Pseudocode?

A pseudocode is a plain English representation of a program or algorithm which specify the flow and operation of the program. It generally used to represent the structural flow of a program, and it is not associated with any specific programming language as it is not associated with any programming language which makes it a perfect tool to represent algorithms for various problems.

Vamware

There are some significant points you need to know about pseudocode.

  • It is not a programming language.
  • It is just a learning and reasoning tool, which is used by programmers and developer to put an underlying about how to write the actual code.
  • Pseudocode can not be executed or compiled by any compiler, interpreter or assembler.
  • Unlike programming language code, pseudocode does not follow a strict structure and syntax. Here the programmer can write the code syntax as he pleased.

Example

Code to check  if the user entered number is odd or even:

C++

int main()
{ 
int num;
cout<<"Enter a number";
cin>>num;

if(num%2==0)
    cout<<"Even Number";

else
    cout<<"Odd Number";
}

Pseudocode

num :INPUT “Enter a number”
IF num MOD 2 ===0
    print “Even Number”
ELSE
    print “Odd Number”

Why use pseudocode?

Before building anything, we first need to create a blueprint which describes all the methods, strategies, flow of structure and the resulting interface of the actual project. Developers and programmers follow the same concept before they start writing the code for their projects, but here instead of blueprint the developer use the pseudocode to represent what approach and structure the actual program goanna follow.

The syntax of every programming language varies from each other, which make it hard to understand the implemented algorithm, but pseudocode provides a solution for this. With the help of pseudocode concept, we can write an algorithm using simple English, so a specific programming language developer or programmer could understand the algorithm and implement it using the high-level programming language.

With pseudocode, we deal with the actual logic, and the basic operations provided by every programming language. While writing pseudocode, we focus on the atomic process and methods provided by every programing language and based on those operating and, methods we try to build an algorithm. When we write pseudocode for an algorithm, we mention only those operations, operators and methods which are common to every programming language.

How to write a Pseudocode?

As pseudocode does not follow a strict systematic or standard way of being written, so don’t think writing pseudocode as a rule. However, there are some standard conventions that every programmer follows while writing one.

Convention to write pseudocode:

  • Use capital words for reserved commands or keywords, for example, if you are writing IF…ELSE statements then make sure IF and ELSE be in capital letters.
  • Write only one statement per line.
  • Use indentation for block body. It keeps the body of every component isolated and indenting different pieces of each block will indicate that those pieces of pseudocode go under a less intended section.
  • Be specific while writing a statement, use plain English to provide a particular description.

Pseudocode for different statements:

Operators:

1. Assignment Operator:

=, <- or :=

2. Comparison Operator:

== , !=, <, >, <= , and >=

3. Arithmetic Operator:

+,-, *, /, MOD(%)

4. Logical Operator:

AND, NOT and OR

5. Sum, Product:

Σ Π

Special Keyword:

  1. START: To begin the pseudocode.
  2. INPUT: Take input from the user.
  3. PRINT: To print the output on the screen.
  4. READ/GET: Input format while reading data from the file.
  5. SET, INIT: Initialize a value
  6. INCREMENT, BUMP: Increase the value of the variable, equivalent to a++
  7. DECREMENT: Decrease the value of the variable, equivalent to a—.
  8. COMPUTE, CALCULATE, DETERMINE: To calculate the expression result

Conditional Statements:

if

IF condition
    THEN if body
ENDIF

if…else

IF condtion THEN
    if body
ELSE
    else body
ENDIF

if…else if….else

IF condition statement THEN
    if body
ELSE IF condition THEN
    else if statement
ELSE
    else body
ENDIF

Example1 :

age = INPUT : “Enter your age”
IF age is greater than 18
    PRINT “adult”
ELSE
    PRINT “Under age”

Example 2:

age = INPUT : “Enter Your age”

IF age is equal to 18 THEN
    PRINT “under check”
ELSE IF age is greater than 18
    PRINT “Give entry”
ELSE
    PRINT “under age”
ENDIF

Iterators:

for loops:

FOR initial_value TO end_value
    for body
ENDFOR

Example:

FOR i -> 0 to 20
    PRINT i
ENDFOR

while loop

WHILE condition
    while body

ENDWHILE

Example:

i : 0
WHILE i  <= 20
    PRINT i
ENDWHILE

Functions

FUNTION function_name(parameters)
    function body
    RETURN value

ENDFUNCTION

Example:

FUNTION add( para1, para2 )
    result: para1+para2
    RETURN result
ENDFUNCTION add

Write the FizzBuzz algorithm using pseudocode

FizzBuzz is a standard coding and interview problem, in which we write a code to print Fizz, Buzz and FizzBuzz when the multiple of 3, and 5 occur.

The Problem state:

  • Write a code that prints each number from 1 to 30 on a new line.
  • Print “Fizz” if the number is the multiple of 3.
  • Print “Buzz” if the number is multiple of 5
  • For number which is multiple of both 3 and 5 print “FizzBuzz”

FizzBuzz Pseudocode

num : 1
FOR num -> 1 to 20
    IF num MOD 15 ===0
        PRINT “FizzBuzz”
    ELSE IF num MOD 3 ===0
        PRINT “Fizz”
    ELSE IF num MOD 5===0
        PRINT “Buzz”
    ELSE
        PRINT num
    ENDIF
ENDFOR

Equivalent Python Code:

for num in range(1, 21):
    if num % 15 ==0:
        print("FizzBuzz")
    elif num %3 ==0:
        print("Fizz")
    elif num %5==0:
        print("Buzz")
    else:
        print(num) 

Advantage of Pseudocode

  • It increases the code readability.
  • Provide the best way to write algorithms.
  • More than specific syntax focus on program logic.
  • The best approach to set an underlying about how the actual program will be written.

Conclusion

There are no such strict rules to write pseudocode, the programmer can write it as he/she wishes, but it should be written in such a manner so other developers could read and understand the algorithm. All the examples and syntax we have mentioned here are conventional, and generally, most of the programmer follows the same syntax, and if you want, you can also modify it. In the above example, you can notice that for each component end we use the END word if you want you can replace it with { } curly braces.

If you like this article or have any suggestions, please let us know. We hope you learned something new from this.

You might be also interested in:

Leave a Reply

Your email address will not be published. Required fields are marked *