Pseudo Code

What is pseudo code?

“Pseudocode is an informal high-level description of the operating principle of a computer program or other algorithm. It uses the structural conventions of a programming language, but is intended for human reading rather than machine reading.”

Rules for pseudo code

  1. Write only one stmt per line

Each stmt in your pseudocode should express just one action for the computer. If the task list is properly drawn, then in most cases each task will correspond to one line of pseudocode.

  1. Capitalize initial keyword

In the example above, READ and WRITE are in caps. There are just a few keywords we will use:

READ, WRITE, IF, ELSE, ENDIF, WHILE, ENDWHILE, REPEAT, UNTIL

  1. Indent to show hierarchy

We will use a particular indentation pattern in each of the design structures:

SEQUENCE: keep statements that are “stacked” in sequence all starting in the same column.
SELECTION: indent the statements that fall inside the selection structure, but not the keywords that form the selection
 LOOPING: indent the statements that fall inside the loop, but not the keywords that form the loop
  1. End multiline structures

See how the IF/ELSE/ENDIF has constructed above. The ENDIF (or END whatever) always is in line with the IF (or whatever starts the structure).

  1. Keep stmts language independent

Resist the urge to write in whatever language you are most comfortable with. In the long run, you will save time! There may be special features available in the language you plan to eventually write the program in; if you are SURE it will be written in that language, then you can use the features. If not, then avoid using the special features.

Advantages and disadvantages

Pseudocode Disadvantages

  • It’s not visual
  • There is no accepted standard, so it varies widely from company to company
  • Pseudocode Advantages Can be done easily on a word processor
  • Easily modified
  • Implements structured concepts well
  • Flowchart Disadvantages Hard to modify
  • Need special software (not so much now!)
  • Structured design elements not all implemented
  • Flowchart Advantages

Standardized: all pretty much agree on the symbols and their meaning

  • Visual (but this does bring some limitations)

Calculation symbols

We will often have to represent an expression like the one that computes grossPay. To symbolize the arithmetic operators we use these symbols

Grouping         ( )

Exponent       ** or ^

Multiply          *

Divide                /

Add                    +

Subtract            –

There is a precedence or hierarchy implied in these symbols.

Order of execution

( )         equations in parenthesis

**         exponentiation

/ *        division and multiplication

+ –       addition and subtraction

Note: when operators of equal value are to be executed, the order of execution is left to right.

Selection

When we have to make a choice between actions, we almost always base that choice on a test. The test uses phrases like “is less than” or “is equal to”. There is a universally accepted set of symbols used to represent these phrases:

>        (greater than)

<        (less than)

>=       (greater than or equal to)

<=      (less than or = (equal to)

<>       (not equal to)

It is interesting to notice that these can be paired up:

 SYMBOL        IS OPPOSITE TO

>                         <=

<                              >=

=                             <>

Logical operators: AND, OR, NOT

  • AND
  • OR
  • NOT