DEV Community

Angel Oduro-Temeng Twumasi
Angel Oduro-Temeng Twumasi

Posted on • Updated on

Demystifying Pseudocode: A Practical Guide with Test Examples βœπŸΎπŸ“

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

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

Problem Statement 1
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

Problem Statement 2
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

Problem Statement 3
Write an algorithm to print the first 20 numbers in the Fibonacci sequence (Read about the 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

Problem Statement 4
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 and LinkedIn

Top comments (0)