How to Write Pseudocode? A Beginner's Guide with Examples

Posted in

How to Write Pseudocode? A Beginner's Guide with Examples
vinaykhatri

Vinay Khatri
Last updated on December 22, 2024

    Generally, developers go through several phases, from idea initiation to turning it into a working implementation. Though reaching the working implementation is extremely complex, an algorithm and pseudocode can significantly help them.

    But how? An algorithm and pseudocode help developers break down a problem into small, simple tasks, allowing them to solve each quickly and easily. In addition, they make it possible to detect errors even before writing the actual code.

    If you are a novice to programming, you might be wondering how to write a pseudocode. Well, this blog post will help you know in detail.

    Before diving into the code, let’s discuss what pseudocode is and why we need it.

    What is Pseudocode?

    It is the plain English representation of a computer program or algorithm specifying its flow and operation. It is generally used to represent the structural flow of a program and is independent of any specific programming language. The same quality makes it a perfect tool for representing algorithms for various problems.

    In other words, pseudocode is a technique that lets programmers or developers represent how they will implement an algorithm. Generally, people refer to it 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.

    We can consider pseudocode as an intermediary step between an algorithm and th actual code. It describes the steps of an algorithm informally, i.e., syntax-free. Though it closely resembles the source code, it is not bound to any programming language . It leverages the English language and simple programming constructs, like loops and conditional statements.

    Further, a pseudocode must describe an algorithm so well that code implementation becomes a mere task of translating every single line into code using a specific programming language’s syntax.

    Key Points To Note

    • A pseudocode is not bound to a programming language but is subjective and non-standard.
    • It is just a learning and reasoning tool that programmers and developers use to underline how to write the actual code.
    • You can compile or execute it by any assembler, compiler and interpreter .
    • Unlike a programming language code, it does not follow a strict structure and syntax.

    Pseudocode vs Actual Code

    Let us understand this using an example.

    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

    BGEIN
    
    num = INPUT: "Enter a number"
    
    IF num MOD 2 == 0
    
    print "Even Number"
    
    ELSE
    
    print "Odd Number"
    
    END 

    Why Use Pseudocode?

    Before building a project, we create a blueprint describing all the methods, strategies, structure flow, and the actual project's resulting interface. Developers and programmers follow the same concept before 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.

    Every programming language's syntax varies, making it hard to understand the implemented algorithm by studying the code. But pseudocode provides a solution, as it is independent of programming languages and written in simple English.

    Here are some reasons to use pseudocode:

    • Easier to read: Developers often work closely with other professionals, such as product owners, managers, etc., with little to no programming knowledge. However, using pseudocode makes it easy for them to understand the flow and mechanics of a program’s, website’s, or app’s source code. It facilitates easy communication between different professionals.
    • Simplifies code construction: Code generation becomes extremely easy and quick with pseudocode.
    • Middle point: A middle point between an algorithm and the actual code.
    • Quick bug detection: As it is written in simple English, uncovering bugs without writing code becomes more effortless. Also, you can make changes to it anytime quickly.

    How to Write Pseudocode?

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

    Let us now look at the detailed steps to follow to write good pseudocode:

    • Use a Plain-Text Editor

    Use any plain-text editor like Microsoft Word or Notepad to start writing your pseudocode. A pen and paper would also work. Mac users can use TextEdit as a plain text editor.

    We will use TextEdit for this process. Go to Finder Applications TextEdit .

    Open TextEdit From Finder

    Alternatively, press command and space to highlight the spotlight search, type TextEdit, and hit return .

    Open TextEdit from Spotlight Search

    • Define the Purpose or Goal of the Process

    The first step is clearly defining the goal or purpose of writing a pseudocode. Simply put, define the problem statement for the program you are trying to implement. This will help other people understand the purpose of the process.

    Define the Purpose or Goal of the Process

    • Outline the Steps in a Logical Sequence

    Enlist all the steps sequentially required to implement the program. Ensure to write one action in one line. Likewise, list all tasks in the form of pseudocode, making it easy for developers to translate them into the actual code.

    You can see in the above image that all four actions are represented in four different lines.

    Outline the Steps in a Logical Sequence

    • Use White Space and Indentation

    Use white spaces and indentation with conditional statements and code blocks to make your pseudocode more readable and clean. For instance, consider you started an ‘if’ block. Use indentation and write a set of the required instructions. This helps people understand that these instructions belong to the ‘if’ block.

    Use White Space and Indentation

    • Capitalize Key Commands

    Capitalizing that commands will remain the same in the actual code, making it easy for developers during code construction. In our example, we can capitalize “IF” and “ELSE,” as these commands will remain the same in the actual code.

    Capitalize Key Commands

    • Use Simple Terminology

    While writing the pseudocode, keep the terminology simple and easy to understand, as we did in our example. This makes it easy for beginners to understand a program’s flow. Also, we have used essential programming constructs like “IF” and “ELSE.”

    • Describe the Process Completely

    Describe everything taking place in the process. Avoid using shortcuts, as novices may not grasp them. This means avoiding variables and operators that describe the condition or expression in simple English, as we did in our example.

    Describe the Process Completely

    • Organize into Sections

    Organizing your pseudocode into sections using curly brackets is better if it is lengthy. This prevents any confusion while reading. In addition, you can add comments using “//” to help others know what the code does.

    Organize into Sections

    • Cross Verify

    Once you complete writing, cross-verify whether you have covered every condition, operation, printing statement, etc. Ensure that it is understood by anyone unfamiliar with it is easy to translate into code, describes the complete process and every object used is defined and described well.

    Standard Conventions to Write Pseudocode

    As mentioned above, pseudocode does not follow a strict, systematic, or standard way of writing; don’t think of writing it as a strict rule. However, there are some standard conventions that every programmer follows while writing one. They are as follows:

    • Use capital words for reserved commands or keywords. For example, if you are writing IF…ELSE statements, then make sure IF and ELSE are 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 detailed description.

    Pseudocode for Different Statements

    Operators

    1. Assignment Operator:

    =, <- or :=

    1. Comparison Operator:

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

    1. Arithmetic Operator:

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

    1. Logical Operator:

    AND, NOT and OR

    1. 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 you must 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 a multiple of both 3 and 5, print “FizzBuzz.”

    FizzBuzz Pseudocode

    BEGIN 
    
    num = 1
    
    FOR num -> 1 to 20
    
    IF num MOD 15 == 0 THEN
    
    PRINT "FizzBuzz"
    
    ELSE IF num MOD 3 == 0
    
    PRINT "Fizz"
    
    ELSE IF num MOD 5 == 0
    
    PRINT "Buzz"
    
    ELSE
    
    PRINT num
    
    ENDIF
    
    ENDFOR
    
    END 

    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)

    Output:

    1
    2
    Fizz
    4
    Buzz
    Fizz
    7
    8
    Fizz
    Buzz
    11
    Fizz
    13
    14
    FizzBuzz
    16
    17
    Fizz
    19
    Buzz

    Advantages and Disadvantages of Pseudocode

    Advantages

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

    • Increases the code readability.
    • Provides the best way to write algorithms.
    • More than specific syntax, it focuses on the program logic.
    • Explains what each line of a program should do.

    Disadvantages

    • No proper format to write a pseudocode.
    • Does not provide the visual representation of a program.
    • Still, there is a need to maintain extra documentation.

    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 many developers use before starting a project.

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

    Example

    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 programming language, you want to write its pseudocode.

    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

    C++ Program

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

    Output:

    Enter the length of the rectangle: 60
    Enter the width of the rectangle: 30
    The area of the rectangle is 1800

    Best Practices to Follow While Writing Pseudocode

    • Keep sentences short and avoid using complex structures. Make sure to write cleanly and crisply.
    • 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.

    10 Common Pseudocode Examples Every Programmer Must Try

    Here are some pseudocode examples you can refer to and get an idea of writing on your own:

    1. Add Two Numbers

    BEGIN  
    
    Declare variables num1, num2, and sum
    
    num1 = INPUT: "Enter the first number"
    
    num2 = INPUT: "Enter the second number" 
    
    sum = num1 + num2 
    
    OUTPUT sum 
    
    END

    2. Calculate the Area and Perimeter of a Square

    BEGIN 
    
    Declare variables length, area, and perimeter
    
    length = INPUT: "Enter the side of a square"
    
    area = length * length 
    
    perimeter = 4 * length 
    
    OUTPUT area 
    
    OUTPUT perimeter 
    
    END 

    3. Calculate the Area and Perimeter of a Circle

    BEGIN 
    
    Declare variables radius, area, and perimeter
    
    radius = INPUT: "Enter the radius of a circle"
    
    area = 3.14 * radius * radius 
    
    perimeter = 2 * 3.14 * radius 
    
    OUTPUT area 
    
    OUTPUT perimeter
    
    END

    4. Calculate Sales Tax

    BEGIN 
    
    Declare variables price, taxrate, tax, and total
    
    price = INPUT: "Enter the product price"
    
    taxrate = INPUT: "Enter the tax rate"
    
    tax = price * taxRate/100
    
    total = price + tax
    
    OUTPUT tax 
    
    OUTPUT total 
    
    END

    5. Check if Your are Eligible for a Driving License

    BEGIN 
    
    Declare a variable age
    
    age = INPUT: "Enter your age" 
    
    IF age >= 16 THEN
    
    PRINT "You are eligible for a driving license" 
    
    ELSE 
    
    PRINT "You are not eligible for a driving license" 
    
    ENDIF
    
    END  
    
    

    6. Find the Largest of Three Numbers

    BEGIN 
    
    Declare variables num1, num2, and num3
    
    num1 = INPUT: "Enter the first number"
    
    num2 = INPUT: "Enter the second number"
    
    num3 = INPUT: "Enter the third number" 
    
    IF num1>num2 AND num1>num3 THEN 
    
    PRINT "num1 is the largest number" 
    
    ELSE IF num2>num3 THEN
    
    PRINT "num2 is the largest number" 
    
    ELSE 
    
    PRINT "num3 is the largest number" 
    
    ENDIF
    
    END

    7. Print Number from 1 to 100

    BEGIN 
    
    Declare a variable counter
    
    FOR counter -> 1 to 100 STEP 1 DO
    
    OUTPUT counter 
    
    ENDFOR 
    
    END

    8. Read the First 50 Numbers and Find Their Sum

    BEGIN 
    
    Declare variables counter  
    
    Initialize a variable sum = 0 
    
    FOR counter -> 1 to 50 STEP 1 DO 
    
    sum = sum + counter  
    
    ENDFOR 
    
    OUTPUT sum 
    
    END 

    9. Find the Sum of All Elements of an Array

    BEGIN 
    
    Initialize variables i = 0, n = 5, and sum = 0 
    
    Initialize an ARRAY num = {4, 3, 6, 7, 9} 
    
    FOR i -> 0 to n-1 STEP 1 DO
    
    sum = sum + num[i] 
    
    ENDFOR 
    
    OUTPUT sum 
    
    END

    10. Swap Two Variables

    BEGIN 
    
    Declare variables num1, num2, and temp 
    
    num1 = INPUT: "Enter the first number"
    
    num2 = INPUT: "Enter the second number" 
    
    temp = num1
    
    num1 = num2 
    
    num2 = temp 
    
    OUTPUT num1 
    
    OUTPUT num 2 
    
    END 

    Conclusion

    There are no such strict rules for writing pseudocode, and thus, the programmer can write it as they wish. However, it should be written in such a manner so that other developers can comprehend the algorithm. All the examples and syntax we mentioned here are conventional, and most programmers follow the same syntax. If you want, you can modify the code to your liking.

    In the above examples, you can notice that we use the END word for each component end. But if you want, you can replace it with { } curly braces.

    Please let us know if you like this article or have any suggestions. We hope you learned something new from this.

    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