How to write good pseudocode?

How to write good pseudocode?

4 mins read2K Views Comment
Updated on Oct 3, 2022 10:23 IST

This article includes java pseudocode and rules of writing pseudocode and will also guides you how to write pseudocode.

2022_10_How-to-write-Good-Pseudocode.jpg

Introduction

No matter the programming discipline—app development, data research, or web development—we employ pseudocode. It is a method for describing an algorithm’s phases that anyone with a rudimentary understanding of programming can comprehend. Although pseudocode is indeed a syntax-free explanation of an algorithm, this should accurately explain the logic of that algorithm; hence converting every line of pseudocode into codes by using the syntax of every specific programming language is all that is required to complete the implementation process. This article will discuss How to write good pseudocode for an algorithm. 

Table of contents

What is Pseudocode?

The term “pseudocode” is frequently used in algorithm-based professions such as programming. It is a method that enables the programmer to depict how an algorithm is implemented. It’s the fabricated representation of an algorithm, to put it simply. Pseudo codes are frequently used to depict algorithms because programmers may understand them with any level of programming experience. The term pseudo code refers to a bogus code or a representation of a code that even a layperson with a basic programming understanding can understand.

ArrayList vs. LinkedList
ArrayList vs. LinkedList
ArrayList and LinkedList are linear data structure and are part of collection framework present in java.util packages. In this article, we will briefly discuss the difference between ArrayList and LinkedList...read more
Top 160+ Java Interview Questions and Answers for 2024
Top 160+ Java Interview Questions and Answers for 2024
This article consists of the Top 160 Java interview questions and answers, covering all the important topics such as Java features, Core Java concepts, OOPs concepts, collections, access specifiers, threads, exceptions,...read more
Multithreading in Java
Multithreading in Java
The below article goes through explaining the concepts of Multithreading in Java and its implementation. Let’s begin the tutorial.

Also read: Object-Oriented Programming (OOPs) Concept in Java

Also Read: Difference between JDK, JRE, and JVM

Benefits of Writing Pseudocode

  • It increases the approach’s capacity to be read. It’s among the greatest methods for beginning the design of an algorithm.
  • Serves as a link between the algorithm and the flowchart, and the software. Additionally serves as a rudimentary documentation tool so that when pseudocode is typed out, the program of one programmer can be easily understood. The method of documentation is crucial in organizations. And this is when using pseudocode is essential.
  • Pseudocode’s major objective is to precisely describe what every line of a program should accomplish, making the programming process more efficient for the programmer.

Main Constructs of Pseudocode

At its heart, pseudocode can represent the following six programming constructs: WHILE, SEQUENCE, CASE, REPEAT-UNTIL, IF-THEN-ELSE, and FOR (always written in uppercase). These terms, which are often referred to as keywords, are used to characterize the algorithm’s control flow.

  • Sequence: Sequentially completed linear tasks are represented by it.
  • WHILE: It is a loop that starts with a condition.
  • REPEAT-UNTIL: A loop containing a condition present at the bottom called REPEAT-UNTIL.
  • FOR: an additional looping method.
  • IF-THEN-ELSE: A conditional statement that alters the algorithm’s flow is known as IF-THEN-ELSE.
  • CASE: It is the IF-THEN-ELSE generalization form.

How to write good pseudocode: Writing a Pseudocode

  1. Put the tasks in the proper order, then write the pseudocode.
  2. Start by establishing the primary objective or aim in a pseudocode statement.

The user can use this program to determine whether a number is even or odd.

  1. To better understand the decision-control and execution process, indent the sentences in the same manner as if-else, for, and while loops are done in a program. They significantly increase readability as well.
  2. Start by establishing the primary objective or aim in a pseudocode statement.
The user can use this program to determine whether a number is even or odd.

3. To better understand the decision-control and execution process, indent the sentences in the same manner as if-else, for, and while loops are done in a program. They significantly increase readability as well.

 
if "1"
print response
"I am sample 1"
if "2"
print response
"I am sample 2"
Copy code
  1. Apply the proper naming conventions. The propensity of people is to do as they see things done. The name must be clear and concise since the approach will be identical if a programmer reads pseudocode.
  2. Use the proper sentence case for each type of word, such as CamelCase for methods, upper case for constants, and lower case for variables.
  3. Explain everything that will occur in the actual code. Avoid abstracting the pseudocode.
  4. Use common programming constructs like “if-then,” “for,” “while,” and “cases” the way we do in programming.
  5. Verify that a pseudocode’s parts are complete, definite, and easy to understand.
  6. Avoid writing the pseudocode entirely programmatically. Avoid using too many technical terminologies because it must be easy to grasp, especially for clients or laypeople.

Let us assume we have a Java code below, and we have to write the pseudo code for the same.

 
import java.util.*;
public class LCM1{
private static long
lcmNaive(long num1, long num2)
{
long lowestCommonMultiple;
lowestCommonMultiple = (num1 * num2) / greatestCommonDivisor(num1,num2);
return lowestCommonMultiple;
}
private static long
greatestCommonDivisor(long num1, long num2)
{
if (num2 == 0)
return num1;
return greatestCommonDivisor(num2, num1 % num2);
}
public static void main(String args[])
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the inputs");
long num1 = scanner.nextInt();
long num2 = scanner.nextInt();
System.out.println(lcmNaive(num1, num2));
}
}
Copy code

The pseudocode for the above Java program will be:

This software determines the Least Common Multiple (LCM) for input values that are too long.

 
function lcmNaive(Arg 1, Arg 2){
Calculate the LCM of Arg 1
and Arg 2 by dividing their product by their
Greatest common divisor product
return lowest common multiple
end
}
function greatestCommonDivisor(Arg 1, Arg 2)
{
if Arg 2 is equal to zero
then return Arg 1
return the greatest common divisor
end
}
{
In the main function
print prompt "Input two numbers"
Take the first number from the user
Take the second number from the user
Send the first number and second number
to the lcmNaive function and print
the result to the user
}
Copy code

Final Rules of Writing Pseudocode

  • When appropriate, write things down mathematically.
  • Don’t rely on the norms of programming languages.
  • Your pseudocode shouldn’t need to be explained.
  • When pseudocode is strongly typed, it performs better.
  • Code blocks with indents
  • Highlight the syntax

Conclusion

In this article, we have discussed the ways of writing good pseudocode and several rules that should be followed. The term “pseudocode” is frequently used in algorithm-based professions such as programming. Although pseudocode is indeed a syntax-free explanation of an algorithm, this should accurately explain the logic of that algorithm; hence converting every line of pseudocode into codes by using the syntax of every specific programming language is all that is required to complete the implementation process.This article covers How to write good pseudocode?.If you liked this article then please like and share it with your friends.

Contributed by-Megha Chadha

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio