logo
logo
Sign in

Tutorial to write Pseudocode

avatar
Cloudy Tech
Tutorial to write Pseudocode

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.

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”

How to Write Pseudocode?

Pseudocode should not be considered a strict rule, as it does not follow strict systematic or standardized notation. However, there are some standard writing rules that all programmers follow. These are:

  • Use uppercase letters for reserved commands or keywords. For example, when creating an IF ... ELSE statement, make sure that IF and ELSE are in uppercase.
  • Write only one instruction per line.
  • Use indentation on the block body. Separate the body of each component and indent the different parts of each block to indicate that those parts of the pseudocode fall into unintended sections. 
  • Be specific when writing the
    statement and use simple English to provide a concrete explanation.

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

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

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) 

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 for writing pseudocode, so programmers can write whatever they want, but they need to write it so that other developers can understand the algorithm.

All examples and syntax described here are traditional, and most programmers generally follow the same syntax. You can change the code as needed.

In the pseudocode example above, you can see that we are using the END word at the end of each component. However, you can replace it with curly braces {} if you wish.

 




collect
0
avatar
Cloudy Tech
guide
Zupyak is the world’s largest content marketing community, with over 400 000 members and 3 million articles. Explore and get your content discovered.
Read more