Demystifying Pseudocode: A Practical Guide with Test Examples ✍🏾📝
Tags: algorithms, beginners, tutorial, codenewbie
Published: 2023-06-25 18:49:57

## Introduction
Programming is all about providing solutions to problems. However, you must be able to articulate the procedure you took to solve that particular problem such that everyone understands it. Just as is written in the quote below 👇🏾
> "Programming is the art of telling another human what one wants the computer to do." - Donald Knuth

Pseudocode offers that avenue to explain your thought process even to someone who isn't a programmer.

In this article, we would take a quick glance at pseudocode and how it can make you an effective and efficient programmer 😎

## Pseudocode explained
Pseudocode is a way of expressing programming logic using human-readable language that resembles actual code but is not tied to any specific programming language syntax. Although it is syntax-free, it needs to provide a full description of an algorithm's logic so that moving from pseudocode to actual coding would be merely a task of translating each line into a particular programming language.

## Benefits of Pseudocode
* It allows developers to outline the logical steps of an algorithm used to solve a problem.
* Developers are able to plan and design algorithms before actually coding it. This streamlines the time taken to code.
* It is not tied to any programming language. This allows developers to focus on the logic needed to solve the problem.
* It serves as an excellent teaching tool for beginners to learn programming concepts. Because of its simplified nature, learners are able to grasp the fundamental programming principles before transitioning to code in a language.
* Collaboration is another major benefit of pseudocode. Developers can work in a team and contribute since it provides a common language for discussions and sharing of problem logic

## Building blocks of Pseudocode
To ensure consistency and promote understanding when translating pseudocode into actual code, it is beneficial to adhere to certain principles or guidelines in writing pseudocode. These principles help foster unity in the usage of pseudocode across different developers and aid in its effective translation.
Here are examples of Pseudocode constructs

### To show sequence
- input: READ, OBTAIN, GET            
- output: PRINT, DISPLAY
- compute: CALCULATE, DETERMINE
- initialize: SET, INIT
- add: INCREMENT
- sub: DECREMENT

### Iteration with FOR
FOR bounds of iteration
sequence
ENDFOR

### Looping with WHILE
WHILE _condition_
sequence
ENDWHILE

### Conditional IF, IF-ELSE
IF _condition_ THEN
   sequence1
ELSE
   sequence2
ENDIF

### Repeat Until (otherwise known as Do...While)
REPEAT
sequence
UNTIL _condition_

### Using Cases (better known as dictionary, switch, heap etc.)
CASE _expression_ OF
condition 1: sequence 1
condition 2: sequence 2
......
condition n: sequence n
OTHERS:
_default sequence_
ENDCASE

The above are theoretically the most used once however, you might find yourself using more depending on your specific use case. 
[This PDF would serve as a very useful guide](https://www.bing.com/ck/a?!&&p=bc1afcc25c87b01bJmltdHM9MTY4NzY1MTIwMCZpZ3VpZD0wODM4ZDc3MC00ODJmLTZhOWQtMThjMi1jNDRhNDk4NTZiOTUmaW5zaWQ9NTIwOA&ptn=3&hsh=3&fclid=0838d770-482f-6a9d-18c2-c44a49856b95&psq=pseudocode+constructs&u=a1aHR0cHM6Ly9sc3Jzc3Nncm91cC5maWxlcy53b3JkcHJlc3MuY29tLzIwMjAvMDQvcHNldWRvY29kZS1jb25zdHJ1Y3QtZ3VpZGUtMS5wZGY&ntb=1)

## How to write pseudocodes
1. Keep it simple, concise and readable.
2. Capitalize the initial words for a particular logic as shown in the constructs above.
3. One statement per line
4. Ensure indentation in order to well represent the structure of the code for easy readability.
5. Multiline sections should be ended with the appropriate keywords. Example IF-ELSE statements should always end with ENDIF etc.
6. Your statements should be programming language independent.

## Worked Examples
<u>**Problem Statement 1**</u>
_Write an algorithm to calculate the product of two numbers given by a user_

**Solution**
START

READ number1
READ number2
CALCULATE the product of two numbers given by the user
PRINT the product to the user

END


<u>**Problem Statement 2**</u>
_Write an algorithm that reads 3 numbers and writes them all in a sorted order_

**Solution**
START

READ number1
READ number2
READ number3
IF number1 is greater than number2
   swap number1 with number2
IF number1 is greater than number3
   swap number1 with number3
IF number2 is greater than number3
   swap number2 with number3
ENDIF
PRINT number1, number2, number3

END

<u>**Problem Statement 3**</u>
_Write an algorithm to print the first 20 numbers in the Fibonacci sequence ([Read about the Fibonacci sequence](https://en.wikipedia.org/wiki/Fibonacci_sequence))_

**Solution**
START
SET count to 0
INIT num1 to 0
INIT num2 to 1
PRINT num1
PRINT num2
INCREMENT count by 2

WHILE count is less than 20
   INIT nextNumber to num1+num2
   PRINT nextNumber
   SET num1 to num2
   SET num2 to nextNumber
   INCREMENT count by 1
ENDWHILE
END

<u>**Problem Statement 4**</u>
_Develop an algorithm to calculate a running sum_

You could try your hands on this and post it in the comment section.

In conclusion, pseudocode is very handy in helping you save a lot of time thinking about the logic of your code and just focus on the language syntax.

Congratulations on your journey 🎉
Subscribe and like this blog if you found it helpful.

Let's get interactive on [Twitter](https://www.twitter.com/_angelotheman) and [LinkedIn](https://www.linkedin.com/in/angelotheman)