Pseudocode

When designing a solution to a programming problem it is often useful to plan out your code in order to test whether or not you have considered all the variables and the iterations required to solve your programming problem.

Pseudocode represents an algorithm by using adapted everyday language and common keywords from programming languages. Instructions are easy for us to read and understand, but still look something like the statements available in many programming languages. Pseudocode is not a strict list of commands since it is not meant to be understood by a computer.

There are therefore no universal standards for pseudo-code, but it should follow these guidelines:

Once algorithms are written (in pseudo-code or flow charts) they should be tested using dry-runs. This involves tracing through the entire algorithm from start to finish with sample test data (trace tables)

Once a programmer is happy with the pseudo-code it can then be developed as a working program using the source code of the programming language in use.

Command syntax - Pseudocode

Key words Description
WHILE / ENDWHILE

iterative operation (loop) where a condition is tested at the start of the loop.

Instructions within loop are repeated until condition becomes FALSE.

REPEAT / UNTIL

Iterative operation (loop) where condition is tested at end of loop.

Instructions within loop will be repeated until condition becomes FALSE.

CASE OF Multiple descision (selection) branch based on the value of and expression
READ, OBTAIN, GET Used to input data
PRINT, DISPLAY, SHOW Used to output data
COMPUTE, CALCULATE, DETERMINE Used to carry out calculations
SET, INIT Used to initialise cariables
INCREMENT, ++ / += Used to increment a variable
DECREMENT, -- / -= Used to decrement a variable
IF / THEN / ELSE / ENDIF Used to select different instructions based on result of Boolean test
FOR / NEXT

Iterative Operation (loop) based on a count, rather than a condition.

Instructions within the loop are executed a specific number of times as determined by count index.

Below is a solution for the Guess My Number game.

#Pseudocode for the Guess Number Game
import time and random modules 
number ← random integer (1 – 100) 
guess_count ← 0

OUTPUT Welcome to the Guess My Number program. When prompted enter your guess.

PAUSE (2 seconds)

guess ← INTEGER INPUT I am thinking of a number between 1 and 100, can you guess it?

REPEAT while guess != number:
        IF guess > number:
        guess_count ← guess_count + 1 
        OUTPUT No, that’s not it, mine is lower 
        PAUSE (2 seconds) 
        guess ← INTEGER INPUT I am thinking of a number between 1 and 100, can you guess it?

        ELSE IF guess< number:
            guess_count ← guess_count + 1 
            OUTPUT No, that’s not it, mine is higher 
            PAUSE (2 seconds) 
            guess ← INTEGER INPUT I am thinking of a number between 1 and 100, can you guess it?

OUTPUT Well done, you took (guess_count) guesses 
EXIT.


As you can see this pseudocode breaks down a solution and considers how the variables will be used within our code.