Abstract code on a screen representing operating systems and programming

Session Objectives

  • To write pseudo code.
  • To convert Flowcharts into Pseudo code.
  • To perform walkthroughs on pseudo code.
  • To represent data files in flowcharts and pseudo code.
  • To represent arrays in flowcharts and pseudo code.


Pseudo code

Pseudo code (pseudo = fake, code = computer language) is a fake computer language (3GL). Throughout these discussions, we have been using the Program Development Cycle (PDC) to move from a high-level, general analysis of a problem to a more specific solution.

Characteristics of Pseudo code

There is no real standard for pseudo code; however, it must have the following characteristics:

  • Statements are written in simple English.
  • Each instruction is written on a separate line.
  • Keywords are used to consistently name the parts of control structures.
  • Indentation is used to make parts of a control structure conspicuous.
  • Each set of instructions is written from top to bottom, with only one entry point and one exit point.

Again, the three basic structures are represented:

  • Simple sequence
  • Decision
  • Iteration

Simple Sequence

Assigning Variables

This is where variables are manipulated within your algorithm.
The New Webster's Computer Terms dictionary definition: A variable is a symbolic name representing a value that changes during the program's execution.
Back to Secondary School algebra, remember these?
5 = 5x + 4y
0 = 5(x + y) - 3x + 5
Solve for x and y. x and y are the variables.

Types of Variable Assignments

There are two types of variable assignments:

  1. When you are initializing a variable.
  2. As a result of some processing.

It will be considered BAD FORM not to initialize a variable before using it.

Verbs used for initialization: INITIALIZE, SET.

INITIALIZE counter to zero
SET recordSize to 500

Assignments from Processing

Expect to use the symbol = (will be referred to as an assignment operator).

totalPrice = basePrice + salesTax

You can use this form for initializing variables as well:

counterA = 0
counterB = 0
recordSize = 500

The value on the right side of the = is calculated (if necessary) and then placed into the variable that is on the left side of the =. You can have only one variable on the left of the =.

Flowchart vs. Pseudo code: Assignments

Flowchart Snippet:
Flowchart vs. Pseudo code: Assignments

Pseudo code:

counterA = 0
SET recordSize to 500

Input of Data

Verbs used: READ, GET.

  • READ usually refers to data being read from a file.
  • GET usually refers to data being entered by the user at a keyboard.

Examples:

READ address FROM customerFile
GET newAddress
Flowchart vs. Pseudo code: Input

Flowchart Snippet:
Flowchart vs. Pseudo code: Input

Pseudo code:

READ address FROM customerFile
GET newAddress

Output of Data

Verbs used: PRINT, WRITE, OUTPUT, DISPLAY.

  • PRINT usually refers to data being sent to a printer.
  • WRITE usually refers to data being sent to a file.
  • OUTPUT and DISPLAY usually refer to data being sent to a monitor.

In pseudo code, the output is not necessarily formatted (made to look pretty), so it is common to simply list each piece of information separated by commas. E.g., DISPLAY count, average.

PRINT "Program Completed"
WRITE newAddress TO customerFile
OUTPUT name, address, postalCode
DISPLAY "End of Data Reached"
Flowchart vs. Pseudo code: Output

Flowchart Snippet:
Flowchart vs. Pseudo code: Output

Pseudo code:

WRITE address TO customerFile
DISPLAY newAddress

Computation

Some verbs used: ADD, COMPUTE, CALCULATE, MULTIPLY, DIVIDE, SUBTRACT.

Examples:

ADD 1 TO counter
COMPUTE tax = price x 0.15
degreesC = (degreesF- 32) x (5/9)
Flowchart vs. Pseudo code: Computation

Flowchart Snippet:
Flowchart vs. Pseudo code: Computation

Pseudo code:

ADD 1 TO counter
degreesC = (degreesF- 32) × (5/9)

Module Call

Verbs Used: CALL.

CALL myModule
CALL yourModule(myvar)
CALL theirModule(yourVar, myVar, someVar)

The exception is when you are invoking a function that is returning a value to a variable in the parent flowchart:

var = yourFunction(a, b)
Flowchart vs. Pseudo code: Module Call

Flowchart Snippet:
Flowchart vs. Pseudo code: Module Call

Pseudo code:

CALL dspMenu(x)
x = func(a, b, c)

Selection (Decision)

Simple Decision

The basic form looks like:

IF condition THEN
    statement(s)
ELSE
    statement(s)
ENDIF
  • If the condition is TRUE, then the statements immediately following the THEN keyword are executed, until the ELSE clause. At which point the program resumes after the ENDIF keyword.
  • If the condition is FALSE, then the statements immediately following the ELSE keyword are executed.
    Note the indentation that is being used:
  • Makes it easier to see the components of the selection.
  • Provides for readability.
Flowchart vs. Pseudo code: Simple Decision

Flowchart Snippet:
Flowchart vs. Pseudo code: Simple Decision

Pseudo code:

IF a > b THEN
    c = e + f
ELSE
    c = t x q
ENDIF

Pseudo code Structure:

IF condition THEN
    StatementA
    StatementB
    StatementC
ELSE
    StatementD
    StatementE
ENDIF

Importance of Indentation

An example of no indentation:

IF a = b THEN
DISPLAY "A equals B"
c = (23 - d) / 2
lifeUniverseEverything = 42
ELSE
DISPLAY "A does not equal B"
vogons = 1
earth = 0
ENDIF

With indentation:

IF a = b THEN
    DISPLAY "A equals B"
    c = (23 - d) / 2
    lifeUniverseEverything = 42
ELSE
    DISPLAY "A does not equal B"
    vogons = 1
    earth = 0
ENDIF

Pseudo code:

IF avg < 50 THEN
    DISPLAY "Low average”
ENDIF

Multiway Decision

The basic form looks like:

SWITCH (variable)
    CASE (option)
        statement(s)
    CASE (option)
        statement(s)
    . . .
    CASE (option)
        statement(s)
    DEFAULT
        statement(s)
ENDSWITCH
  • variable is what is being tested.
  • option is the possible value that variable takes on, and represents the new path of logic.
Flowchart vs. Pseudo code: Multiway Decision

Flowchart Snippet:
flowchart

Pseudo code:

SWITCH (usrSel)
    CASE (‘A’)
        res = a + b
    CASE (‘S’)
        res = a - b
    CASE (‘M’)
        res = a × b
    CASE (‘D’)
        res = a ÷ b
    DEFAULT
        DISPLAY "Invalid option”
ENDSWITCH

Iteration (Loop)

Pre-Test Loop

The basic form looks like:

DOWHILE condition
    statement(s)
ENDWHILE
Flowchart vs. Pseudo code: Pre-Test Loop

Flowchart Snippet:
Flowchart vs. Pseudo code: Pre-Test Loop

Pseudo code:

DOWHILE i < 10
    b = b × c
    i = i + 1
ENDWHILE

Post-Test Loop

The basic form looks like:

DO
    statement(s)
WHILE condition
Flowchart vs. Pseudo code: Post-Test Loop

Flowchart Snippet:
Flowchart vs. Pseudo code: Post-Test Loop

Pseudo code:

DO
    b = b × c
    i = i + 1
WHILE i < 10

Pseudo code Walkthroughs

What is the output of the following pseudo code?

num = 1
var = 0
DO
    var = num + var
    DISPLAY var
    num = num + 2
WHILE num < 7

What is the output of the following pseudo code?

i = 5
DOWHILE i > 0
    j = i
    f = 1
    DOWHILE j > 1
        f = f * j
        j = j – 1
    ENDWHILE
    DISPLAY f
    i = i – 1
ENDWHILE
DISPLAY “Done”