NS Diagrams and pseudocode

To effectively design computer programs, tools have been developed to assist in the process. The required tools for this course take two forms. The first is known as Flow Charts and the second is known as pseudocode. All programs can be expressed in either of these forms. You are required to know both.

All computer programs can be expressed using three basic constructs - these are

Both Flow Charts and pseudocode allow you to easilly express these forms.

Flow Charts

Flow charts consist of shapes connected by lines that represent different constructs. These can easily be draw with a pencil in a free-hand form. During the construction process, many changes will need to be made.

Inside the boxes, you can put any of the other processes shown below.

 
The most simplest program just consists of a series of steps that the computer will follow one after the other. This is drawn as a series of trapeziums for I/O and rectangles with each process described inside.

The I/O might include:
Write Amount to accounts file
Read data from user
Get Age of customer

The processes might include:
Set counter to counter + 1
Calculate the diameter of a circle

Common processes and other structures can be grouped together into a common module or subprocess. These can then be given a name and then refered to by that name in a mai process module.

Such sub-processes migh include:
Write Report to Printer
Calculate Tax
Determine amount owed

 
This is the first of the three basic constructs for iteration. It says:
Do the process/processes in the box while some condition is true. Of course, these processes may consist of one or more processes, sub-processes or other constructs.

The implication with this is the condition must be set before the block is entered and it must have the potential to be changed somewhere in the inside processes. As well, the process may not be entered at all.

This is the second of the three basic constructs for iteration. It says:
Do the process/processes in the box until some condition is true. Of course, these processes may consist of one or more processes, sub-processes or other constructs.

The implication with this is the condition is not set before the block is entered and it must have the potential to be changed somewhere in the inside processes. As well, the process must always be carried out at least once.

This is the third of the three basic constructs for iteration. It says:
Do the process/processes in the box a set number of times while the value of the counter changes from the start number to the finish number in increments of 1. Of course, these processes may consist of one or more processes, sub-processes or other constructs.

(Some languages, such as Basic, allow a step in the counting of values other than just 1)

 
This is the first of the decision making constructs. The condition is a predicate, the same as above and is described below.

This says to do the processes in the left box if the condition is true and the right box if the condition is false. The process in the right box can be omitted. This is the same as saying "Do nothing if false". Of course, the process that is done may be any construct defined on this page, so another "If-Then-Else" block may be the process described. When this happens, the situation is known as "nesting" and can become quite complex.

To overcome the problem and complexity of nesting If-Then-Else statements, the construct of Case was provided in more recent implementations of language.

It allows multiple values to be tested and then the appropriate process to be carried out based on the result. If none return a true value, the default process is then carried out. This is usually "do nothing".

Diagrams made using www.gliffy.com


Pseudocode

Pseudocode is an alternate method of representing the same structures as seen with N-S diagrams. The basics of this is that key words are written in uppercase.

www.minich.com/education/wyo/basic/pseudocodeexample.htm

phoenix.goucher.edu/~kelliher/cs17/feb12.html

Process (in sequence)

Examples of processes in pseudocode are as follows. The uppercase words are key words

SET value TO total + 3

WRITE name TO printer

GET balance FROM keyboard

Iteration - Test First

DO WHILE <predicate>

processes

ENDWHILE

Iteration - Test Last

DO

processes

UNTIL <predicate>

Iteration - Fixed Repetition

DO FOR value <- start TO finish

processes

ENDFOR

Decision Making - IF-Then-Else

IF <predicate> THEN

processes

ENDIF

and

IF <predicate> THEN

processes

ELSE

processes

ENDIF

Decision Making - Case

CASE selection OF

item 1 : processes

item 2 : processes

..

item n : processes

ELSE

processes

ENDCASE

Predicates

Predicates are condition statements and rely on Boolean Logic and Algebra. They consist of three parts and always results in the value of True or False.

<Value to test> <Boolean Operator> <Value to test against>

An example might be

Age > 6 where

Predicates can also consist of multiple conditions joined by AND, OR, XOR (and other boolean operators such as NAND and NOR). The following tables will help interpret the final value of complex predicates.

AND

True True True
True False False
False True False
False False False

OR

True True True
True False True
False True True
False False False

XOR (Exclusive OR)

True True False
True False True
False True True
False False False

NOT

True False
False True

Reference:

Kaye B Program Design and Pascal


Author: Mike Leishman