How to Write Pseudocode?

By | February 26, 2021
How to Write Pseudocode?

In this article, we will discuss how to write pseudocode? Continue reading.

Pseudocode is a mix of technical terms and common English that facilitates computer programmers to write computer algorithms. Before diving into how to write pseudocode, let’s first discuss what pseudocode is and why do we need it.

Vamware

What is Pseudocode?

pseudocode is the plain English representation of a computer program or algorithm, which specifies the flow and operation of the program.

It is generally used to represent the structural flow of a program, and it is not associated with any specific programming language. The same quality makes it a perfect tool to represent algorithms for various problems.

Some significant points you need to know about pseudocode are:

  • It is not a programming language.
  • It is just a learning and reasoning tool, which is used by programmers and developers to underline 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 pleases.

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 that describes all the methods, strategies, flow of the 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 the blueprint, the developer uses pseudocode to represent what approach and structure the actual program will follow.

The syntax of every programming language varies, which makes it hard to understand the implemented algorithm by studying the code. But pseudocode provides a solution for this.

With the help of pseudocode, we can write an algorithm using simple English. It allows a specific programming language developer or programmer could to understand the logic of the program and implement it in a specific 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 operations and methods we try to build an algorithm.

When we write pseudocode for an algorithm, there are several operations, operators and methods that we can use. In the section below, we mention only those which are common to every programming language.

How to Write Pseudocode?

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

  • 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 the 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 condition 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 occurs.

The problem states:

  • Write a code that prints each number from 1 to 30 in 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

There are several advantages of leveraging pseudocode while developing computer programs and solving problems. Some of the most important ones are:

  • Increases the code readability.
  • Provides the best way to write algorithms.
  • More than specific syntax focuses on the program logic.
  • The best approach to represent how the actual program will be written.

Conclusion

There are no such strict rules to write pseudocode, and thus, the programmer can write it as she wishes, but it should be written in such a manner so that other developers could comprehend the algorithm.

All the examples and syntax we have mentioned here are conventional, and generally, most of the programmer follows the same syntax. If you want, you can modify the code as per to your liking.

In the above example, you can notice that for each component end, we use the END word. But 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 *