PSEUDOCODE

Last Update October 27, 2006 17:38 by G. Grant

Pseudocode is a structured form of English that is used to describe algorithms. It allows designers to concentrate on the logic of the algorithm.

Although pseudocode resembles some high level programming languages, it should be independent of any specific programming language. You could think of it as generic code that can be translated to any programming language.

Pseudocode is easy to write and read. It lets the program designer concentrate on the logic of a problem without the distraction of conforming to the syntax of any specific programming languge. Algorithms in pseudocode should completely describe the logic for the specific program/function/method/procedure. They should also clearly state any formulas that are part of the algoritm.

Another programmer should be able to implement the algorithms without having to consult the designer. It is not necessary to have knowledge of any specific programming language to write pseudocode.

An individual who understands the problem, but does not necessarily understand programming should be able to read and understand the pseudocode.

Textbooks, designers, and instructors have different styles of pseudocode. Pseudocode is not a rigorous notation, since it is read by other people, not by the computer. There is no universal "standard" for the industry, but software developers, instructors and classes often will agree on a convention that they will all use in order to be consistent. Students in CIS290 and CIS254 should follow the conventions shown here.

 

Program control flow can be reduced to three basic control structures.

SEQUENCE is a series of processing steps where one step follows the previous.

SELECTION is the presentation of a condition and a choice between two actions, the choice depends on whether the condition is true or false.The condition most often involves comparing two variables.

REPETITION a structure where a sequence of instructions are performed continually as long as a condition is true.

 

Some terms to use in Pseudocode

Input: ACCEPT, OBTAIN, READ
Output: DISPLAY, SHOW, PRINT
Compute: COMPUTE, CALCULATE, DETERMINE
Initialize: SET, INITIALIZE
Add one: INCREMENT, BUMP
Subtract one: DECREMENT

 

SEQUENCE Control Structure

Sequential control is indicated by writing one action after another, each action on a line by itself, and all actions aligned with the same indent.

The actions are performed in the sequence (top to bottom) in which they are written. Note that the word begin and end indicate the beginning and end of the block of code. A label startTheDay is used to differentiate the block of code from other blocks of code. It may be the begin and end of a specific program, function, method, or procedure.

General form for the sequence control structure

BEGIN

statement1
statement2
...
statementN

END

Examples:

BEGIN startTheDay

Get out of bed
Use bathroom
Eat breakfast
Brush teeth
Take shower
Get dressed
Smile at self in mirror

END startTheDay

BEGIN calculateRectangleArea

READ height of rectangle
READ width of rectangle
COMPUTE area as height times width

END calculateRectangleArea

Note that the block of statements between BEGIN and END is indented.

Selection Control Structure

IF-THEN

Binary choice on a given Boolean condition is indicated by the use of four keywords: IF, THEN,and ENDIF.

The general form is:

IF (condition) THEN

statementBlock

ENDIF

Note that the code within the if structure is indented, more than one line of code can be contained in the body of the if statement

Example

IF age > 65 THEN

display "Time to retire"

ENDIF

 

IF-THEN-ELSE

Binary choice for a given Boolean condition is indicated by the use of four keywords: IF, THEN, ELSE, ENDIF or ENDIFELSE.

The general form is:

IF condition THEN

statementBlock

ELSE

alternativeStatementBlock

ENDIFELSE

Example

IF hoursWorked > normalWorkWeek THEN

Display overtime message

ELSE

Display regular time message

ENDIF

 

Switch or Case Statement ( another selection structure)

A CASE structure is a multiway branch based on conditions that are mutually exclusive.

The singleVariable must always be a discrete ordinal type such as integer or char

Four keywords, CASE, OF, OTHER, and ENDCASE, and conditions are used to indicate the various alternatives.

The general form is:

CASE OF singleVariable

VALUE_1 : statementBlock_1
VALUE_2: statementBlock_2
...
...
...
VALUE_N: statementBlock_n
OTHER: statementBlock_other

ENDCASE

Example:

CASE OF gradeCode

'A' : points = 3
'B' :points = 2
'C' : points = 1
'D' : points = 0
other: display error message

ENDCASE

Repetition Control Structure

WHILE

The WHILE construct is used to specify a loop with a condition or test at the beginning of the loop. It is sometimes referred to as a Pre-Test loop. The loop is executed as long as the condition is evaluated to true.
The beginning and ending of the loop are indicated by two keywords WHILE and ENDWHILE.
The statement block must contain an updater that will cause the condition to evaluate as false at some point in execution in order to avoid creating persistent (infinite) loop.

The general form is:

initializer

WHILE condition

statementBlock
updater

ENDWHILE

Example:

numberOfStudents = 0;
sum = 0;
average = 0;

WHILE numberOfStudents < 35

accept testScore
add testScore to sum
increment numberOfStudents by 1

ENDWHILE

Note:

  • a while loop usually requires inializing some variables prior to the loop (sometimes called priming the loop)
  • a while loop may not be executed if the condition evaluated to false the first time it is encountered
  • in some languages this may be called DOWHILE

DO-WHILE

This loop is similar to the WHILE loop except that the test is performed at the bottom of the loop instead of at the top. The DO-WHILE loop is sometimes called a Post-test loop.

Two keywords, DO and WHILE are used to specify this loop. In some languages this structure uses the key word REPEAT instead of DO, and UNTIL instead of WHILE and is called a REPEAT-UNTIL structure.

The general form is:

DO

statementBlock
updater

WHILE condition

Note:

  • In a DO-WHILE loop, the initializer may be contained prior to the loop or in the body of the loop.
  • The statements in a DO-WHILE loop will always be executed at least once

Example:

DO

accept input from user
evaluate input

WHILE input is not blank

FOR loop

This loop is a specialized control structure for iterating a specific number of times. It is often called a "counting" loop. Two keywords, FOR and ENDFOR are used. The heading or first line of the for structure includes an initialization statement, a test condition, and an update.

The general form is:

FOR (initialization, test condition, updater)

statementBlock

ENDFOR

Example

FOR (month = 1; month <= 12; month+1)

display monthName

ENDFOR

or

FOR month = 1 through 12

display monthName

ENDFOR


NESTED Control Structures

The control structures can be embedded within each other, and this is made clear by use of the key words and indenting.
Nested constructs should be clearly indented from their surrounding constructs.

Example

SET totalFreezeDays to zero
DO

READ Temperature

IF Temperature > Freezing THEN

INCREMENT totalFreezeDays

END IF

WHILE Temperature < zero
Print totalFreezeDays

 
Reference: Simple Program Design by Lesley Anne Robertson