How to Write Pseudocode?

Posted in

How to Write Pseudocode?

Vinay Khatri
Last updated on November 9, 2022

    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 we need it.

    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 for representing algorithms for various problems. Alternatively, we can say that pseudocode is a technique that lets programmers or developers represent how they will implement an algorithm.

    Generally, people refer to pseudocode as false code. This means that it is not an actual code. Instead, it is the representation of code that every common person with basic programming knowledge can understand it.

    Some significant points you need to know about pseudocode are as follows:

    • 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.

    Pseudocode vs Actual Code

    Let us see how the actual code and a pseudocode for a specific problem looks like. Consider we need to check whether the given number is odd or even.

    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, developers use 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 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 of every programming language .

    While writing pseudocode, we focus on the atomic process and methods provided by every programing language. 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 writing, don’t think of writing pseudocode as a strict rule. However, there are some standard conventions that every programmer follows while writing one. Let us discuss those conventions in the later section.

    To write pseudocode, you need to leverage the same logic and conventions analogous to programming code. The only difference is that pseudocode eliminates the need to use a strict syntax of a programming language for the computer system to compile it.

    Here are some of the essential steps that you must follow to write good pseudocode:

    • Initially, write a statement that clearly defines the goal of writing pseudocode.
    • Outline the steps required to perform in a logical sequence.
    • Make sure to indentation while using conditional statements.
    • Leverage programming conventions to name commands and appropriate formats.
    • Explain everything in code using notations as you progress forward.
    • Proofread code to ensure that it is clear and easy to comprehend. It should be understandable even by people from non-technical backgrounds.

    Standard Conventions 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 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

    1. if

    IF condition
        THEN if body
    ENDIF

    2. if…else

    IF condition THEN
        if body
    ELSE
        else body
    ENDIF

    3. 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

    1. for loops:

    FOR initial_value TO end_value
        for body
    ENDFOR

    Example:

    FOR i -> 0 to 20
        PRINT i
    ENDFOR

    2. 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 need to 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 a multiple of 5.
    • For a number that 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) 

    Advantages 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 and wondering what pseudocode is, what syntax it has, 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. So, instead of just getting started with a new programming language, make sure to learn pseudocode.

    Pseudocode Examples for Beginners

    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;
    }

    Check out more pseudocode examples to develop your pseudocode writing skills.

    Best Practices to Follow While Writing Pseudocode

    Some of the best practices to follow while writing pseudocode are as follows:

    • Keep sentences short and avoid using complex structures. Make sure to write clean and crisp pseudocode.
    • Refer to the flowchart for writing pseudocode to ensure the correct flow. Don't create any holes in the flow of logic.
    • Follow programming structure and formatting for easy transition of pseudocode into the actual code.

    Conclusion

    There are no such strict rules for writing pseudocode, and thus, the programmer can write it as she wishes, but it should be written in such a manner so that other developers can comprehend the algorithm. All the examples and syntax we have mentioned here are conventional, and generally, most programmer follows the same syntax. If you want, you can modify the code 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. This article presented you with the details of pseudocode and ways to write one.

    People are also reading:

    FAQs


    Pseudocode is not bound to any programming language and does not have any strict syntax. You can write pseudocode in simple English. However, you must be aware of the commonly used keywords, constructs, and conventions for writing pseudocode.

    Pseudocode involves representing a computer program or algorithm in a simple English language without any strict syntax. Meanwhile, a flowchart is a graphical or pictorial representation of an algorithm (in general, a pictorial representation of a solution model for a problem).

    Source code is something that you need to write in a specific programming language using its syntax and is executable. On the flip side, pseudocode does not have any strict syntax and is not executable.

    No, an algorithm is a set of sequential instructions to solve a specific problem, while pseudocode is the representation of an algorithm that uses an informal way.

    Some popular alternatives to pseudocode are flowcharts, Unified Modeling Language (UML) charts, and Drakon charts.

    Leave a Comment on this Post

    0 Comments