How to Write Pseudocode?

By | October 28, 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++ Programming

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.

How to Write Pseudocode as a Beginner

If you are a complete beginner in the programming world, just getting started with a new programming language, heard of Pseudocode, and wondering what this syntax is and how to write it. Don’t worry it is not a mandatory syntax it’s just a good coding practice, that many developers use before they get started on a project. Pseudocode is mostly used in a project algorithm discussion when developers prefer the common syntax of pseudocode to write the algorithm rather than writing it in a Programming language.

Pseudocode Examples for Beginner

Let’s discuss another pseudocode example that is more beginner-friendly. Suppose you need to write a program in C++ that can calculate the area of a rectangle. But before you implement the code in the programing language you want to write its pseudocode.

Pseudocode Example to calculate the area of a rectangle 

BEGIN
    length = INPUT: "Enter the length of the rectangle"
    width  = INPUT: "Enter the width of the rectangle"

    area = length * width

    OUTPUT area
END

Pseudocode to C++

Now let’s convert the above pseudocode to the equivalent C++ code.

#include <iostream>
using namespace std;

int main() {
    float length, width, area;
    cout<<"Enter the length of the rectangle: "; cin>>length;
    cout<<"Enter the width of the rectangle: "; cin>>width;
    
    area = width*length;
    
    cout<<"The area of the rectangle is "<<area;
}

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 pseudocode examples, 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.

People are also reading:

4 thoughts on “How to Write Pseudocode?

  1. Tim Henwood

    some examples show ===0 and some show ==0. Are these typos or does 3 = have a different meaning than 2 =?

    Reply
    1. Singh Post author

      Pseudocode does not have a rigid syntax and when it comes to comparison operator developer prefer to use different syntax.

      for example
      with if..else statement we can either use single (=), double (==) or triple equal to operator (===) and they all represent the same meaning (Comparison operator).
      example
      IF (a=b) // comparison operator
      IF(a==b) // comparison operator
      IF(a===b) // comparison operator

      But in many cases when we want to find or store Boolean value by comparing two numbers there we can not use single equal to operator as a comparison operator. In that case we have to use double or triple equal to operator to represent comparison operator.
      a = b // assignment operator
      a==b // comparison operator
      a===b // comparison operator

      Many programming languages also support “===” comparison operator as strict equality operator that not only compare the value but also the data type of the operands. So insted of using double (==) operator programmer prefer using === operator for a proper comparison test.

      Thats why in many pseudocode example programmer use === operator as a comparison operator insted of == or =.
      === operator represent that you want a strict equality check. where as == or = operator can be confused with only data comparison test and assignment operator.

      Reply
  2. Graham Smallwood

    It’s cool that you agree that pseudocode isn’t strictly defined, and your text explanations are great. But your examples don’t follow your own text and I think they are actively wrong. The whole point (and you agreed) is to plan out the code before you start. But your examples of pseudocode ARE code. They are a made up language, but you have if-elses and whiles and stuff so it’s a coding language. If you are going to write the entire program, why not just actually start with the destination language?

    For pseudocode to be useful at all, it has to be done before any code. Like a plain english outline that you could read to someone else and they’d have enough information to write the program. It’s the problem solving step before the code step. Your examples just do two code steps in a row, so what’s the point?

    So I’m not trying to debate the meaning of the vague and meaningless word “pseudocode”, but I am pointing out that it needs to be useful or why do it.

    Reply
    1. Singh Post author

      Thanks for your feedback. The Pseudocode syntax that I have mentioned in the article does incline toward programming languages syntax. This is because the final objective of a Pseudocode is how we can implement the algorithm in the programming languages. The debate on what is the absolute correct way to write a Pseudocode is never-ending. And I am appreciated by your response to this article.

      Reply

Leave a Reply

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