Programming Logic                                      Pseudocode Structures

 

Structure

Pseudocode Format

Example

Sequence

One statement or structure  follows another

Statement 1

Statement 2

.

.

statement n

Total = 0

Add 1 to COUNT

Flag = ‘YES’

.

.

Decision

IF-THEN or

IF-THEN-ELSE

IF condition(s)

     Statement(s) to be executed for TRUE

ENDIF

 

IF condition(s)

     Statement(s) to be executed for TRUE

ELSE

     Statement(s) to be executed for FALSE

ENDIF

IF SALES > 2000

     Add 1 to COUNT

     Add SALES to TOTAL

ENDIF

 

IF EOF or COUNT > 500

     FLAG = ‘YES’

ELSE

     DO Process-Procedure

ENDIF

Loops

DO-WHILE or

DO-UNTIL

DO loop-name WHILE condition(s)

     Statement(s) to be repeated

     while condition is TRUE

ENDO

 

DO loop-name UNTIL condition(s)

     Statement(s) to be repeated

     until condition is TRUE

ENDO

DO Detail-Loop WHILE not EOF

     Print Name

     Read Name, Amount

ENDO

 

DO Search-Loop UNTIL (EOF or I > 500)

     READ DATA

     IF not EOF

          IF DATA = SEARCH-DATA

               PRINT “Found!”

               FLAG = ‘YES’

          ENDIF

     ENDIF

ENDO

Case

For 3 or more alternatives to one decision

CASE data field

     WHEN condition-1

          Statement(s)-1 to be executed

     WHEN condition-2

          Statement(s)-2 to be executed

     WHEN condition-3

          Statement(s)-3 to be executed

     ELSE

          Default statement(s)

ENDCASE

CASE room type

     WHEN single

          Price = $105

     WHEN double

          Price = $85

     WHEN family

          Price = $65

     ELSE

          Print “Invalid Room Type”

ENDCASE

 

Pseudocode Rules

1.        The main program is a sequence structure at the left margin headed by START and terminated by STOP.

2.        Each IF is terminated by an ENDIF.  IF, ELSE, and ENDIF are placed at the same margin.

3.        Each DO containing embedded statements is terminated by an ENDO.

4.        Statements belonging to an IF, ELSE, or DO are indented between the heading and the corresponding END scope-terminator to make the pseudocode easy to read and understand.

5.        Pseudocode is language-independent – avoid the use of words peculiar to a specific programming language.

6.        While punctuation (such as terminating an instruction) is optional, uppercase is suggested for keywords: IF, ELSE, DO, END-terminators, WHILE, UNTIL, START, and STOP.