Pseudo Code Guide


Home

Contents


Introduction

Pseudo code (or Structured English) is used to specify program logic in a (somewhat) English like manner, that aims to be independent of any particular programming language. This simplifies program development by separating it into two main parts, logic design and coding. The use of pseudo code allows the programmer to focus on the logic of the program rather than implementation details such as how data is displayed Once the logic is developed, coding becomes the translation of the pseudo code into the required programming language.

Operators

Operators are used to perform operations on one or more values called operands. An example of an operator is "+", the arithmetic operator for addition. In the following expression, 3 and 2 are operands and + is an operator:  x = 3 + 2.

Arithmetic Operators

Arithmetic operators are used to perform calculations.

Assume x is 2 in the following examples.

Operator Description Example Example Result
() Brackets    
- Negation y = -x (-2) is -2
* Multiplication y = x * 3 (2 * 3) is 6
/ or ÷ Division y = 7 / x (7 / 2) is 3.5
+ Addition y = x + 3 (2 + 3)  is 5
- Subtraction y = x - 3 (2 - 3) is -1

Comparison Operators

Comparison operators are used to compare two values. Assume x is 2 and y is 3 in the following examples.

Operator Description Example Example Result
=
Equal
x = y
(2 = 3) is False
≠ or <> Not Equal x ≠ y (2 ≠ 3) is True
< Less Than x < y (2 < 3) is True
≤ or <=
Less Than or Equal To x ≤ y (2 ≤ 3) is True
>
Greater Than
x > y
(2 > 3) is False
≥ or >=
Greater Than or Equal To x ≥ y
(2 ≥ 3) is False

Logical Operators

Logical operators are used to combine or modify sub-conditions. Assume x is 2 and y is 3 in the following examples.

Operator
Description
Example
Example Result
NOT The result is the opposite of the operand NOT (x ≤ y) NOT (2 ≤ 3) is (Not True) is False
AND The result is True if both operands are True otherwise the result is False. x > 7 AND y = 12 (2 > 7 AND 3 = 12) is (False AND False) is False
OR The result is False if both operands are False otherwise the result is True. x ≠ y OR y ≥ 10 (2 ≠ 3 OR 3 ≥ 10) is (True OR False) is True

Sequence

Assignment

Format

variable = expression

Examples

total = 0
y = x * x + z / 4 - 1
name = "Joan Smith"
validLength = True

Input

Format

Input variable, variable, ...

Examples

Input custName
Input distance, speed

Display

Format

Display value, value, ...

Examples

Display "Hello World"
Display totalPrice, taxPayable
Display "Customer Number: ", custNum, "Name: ", custName

Selection

If-Else

Format

IF condition THEN
    statement
    statement
    ...
ENDIF
 

IF condition THEN
    statement
    statement
    ...
ELSE
    statement
    statement
    ...
ENDIF
 

IF condition THEN
    statements
ELSE
    IF condition THEN
        statements
    ELSE
        IF condition THEN
            statements
        ELSE
           statements
        ENDIF
    ENDIF
ENDIF
 

IF condition THEN
    statements
ELSE IF condition THEN
    statements
ELSE IF condition THEN
    statements
ELSE
    statements
ENDIF

Note: the Else part is optional. Use it where required by the logic.

Examples

IF lineNumber > 50 THEN
    Display ""
    Display "Student Number", "Student Name"
    lineNumber = 0
ENDIF
 

IF monthNumber >= 1 AND monthNumber <= 12 THEN
    Display "valid month"
ELSE
    Display "invalid month"
    Display "Month must be between 1 and 12"
ENDIF
 

IF mark >= 80 THEN
    grade = "A"
    comment = "Excellent"
ELSE
    IF mark >= 70 THEN
        grade = "B"
    ELSE
        IF mark >= 60 THEN
            grade = "C"
        ELSE
            IF mark >= 50 THEN
                grade = "D"
            ELSE
                grade = "N"
                comment = "Poor"
            ENDIF
        ENDIF
    ENDIF
ENDIF
 

IF mark >= 80 THEN
    grade = "A"
    comment = "Excellent"
ELSE IF mark >= 70 THEN
    grade = "B"
ELSE IF mark >= 60 THEN
    grade = "C"
ELSE IF mark >= 50 THEN
    grade = "D"
ELSE
    grade = "N"
    comment = "Poor"
ENDIF

Iteration (Repetition)

For

Format

FOR counter = start-value to end-value DO
    statement
    statement
    ...
ENDFOR

Example

FOR x = 1 to 10 DO
    xSquared = x * x
    Display x, xSquared
ENDFOR

While

Format

WHILE condition DO
    statement
    statement
    ...
ENDWHILE

Example

count = 1
WHILE count <= 10 DO
    Display count
    Add 1 to count
ENDWHILE

Programs

Format

program-name()
    statement
    statement
    ...
STOP

Example

addTwoNumbers()
    Input number1, number2
    sum = number1 + number2
    Display sum
STOP 

Subprograms: Subroutines and Functions

The pseudo code for  subroutines and functions are placed below the pseudo code for the program.

Subroutines

Subroutines are also called procedures or void methods. Subroutines do not return a value.

Format - subroutine calls

subroutine-name()
subroutine-name(parameter, parameter, ...)

Examples - subroutine calls in a program

demoProgram()
    ...
    displayTenStars()

    displayAverage(count, total) 
STOP

Format - subroutine declarations

subroutine-name()
    statement
    statement
    ...
EXIT

subroutine-name(parameter, parameter, ...)
    statement
    statement
    ...
EXIT

Examples - subroutine declarations

displayTenStars()
    FOR numStars = 1 to 10 DO
        Display "*"
    ENDFOR
    Display ""
EXIT

displayAverage(count, total)
    IF count > 0 THEN
        average = total / count
        Display average
    ELSE
        Display "Error, nothing to average"
    ENDIF
EXIT

Functions

Functions are also called non-void methods. Functions return a single value.

Format - function calls

function-name()
function-name(parameter, parameter, ...)

Examples - function calls in a program

demoProgram()
    ...
    month = inputMonth()

    IF oddNumber(month) THEN
        ...

    ENDIF
    volume = 4 /3 * PI * cube(radius)
    max = maximumNumber(a, b)
STOP

Format - function declarations

The following are called functions. They return a single value.

function-name()
    statement
    statement
    ...
RETURN with value

function-name(parameter, parameter, ...)
    statement
    statement
    ...
RETURN with value

Examples - function declarations

inputMonth()
    Input monthNumber
    WHILE monthNumber < 1 OR monthNumber > 12 DO
        Display "Error invalid month number"
        Input monthNumber
    ENDWHILE
RETURN with monthNumber

oddNumber(num)
    oddNumber = false
    IF num modulus 2 = 0 THEN
        oddNumber = true
    ENDIF
RETURN with oddNumber

cube(number)
    cube = number * number * number
RETURN with cube

maximumNumber (number1, number2)
    IF number1 >= number2 THEN
        maximumNumber = number1
    ELSE
        maximumNumber = number2
    ENDIF
RETURN with maximumNumber

Keywords

The following words have a special meaning in Pseudo Code (in this subject) and should only be used for the purposes described above.

AND, DO, ELSE, ENDIF, ENDFOR, ENDWHILE, EXIT, FOR, IF, NOT, OR, RETURN, STOP, THEN, TO, WHILE.

Other statements: Display, Input
Operators:  =, +, -, *, /, (), <, <=, >, >=, []


Written by Tim Whitfort.