Algorithms and pseudocode

An algorithm is a step–by–step procedure for solving a problem; programming languages are essentially a way of expressing algorithms.

Here's an example algorithm.

BEGIN
  Check weather
    IF Raining
        Take Bus to school
    ELSE Dry
        Walk to school
    ENDIF
END

Before we get on to programming languages we need to deal with how to write logical and efficient algorithms. Logical is basically ensuring everything makes sense and we achieve the required outcomes. Efficient means using the minimum resources, such as CPU cycles (time) and memory (space).

We can write these in pseudocode (as above) or use flowcharts, we'll cover both, but we will mainly be using pseudocode throughout this course.

Here are the pseudocode guidelines:

The pseudocode keywords are:

for each procedure or subroutine

BEGIN name
END name

for binary selection

IF condition THEN
    statements
  ELSE
    statements
  ENDIF

for multi-way selection

  CASEWHERE expression evaluates to
    A: process A
    B: process B
    …
  OTHERWISE: process …
  ENDCASE

for pre-test repetition

  WHILE condition
      statements
  ENDWHILE

for post-test repetition

  REPEAT
    statements
  UNTIL condition

for FOR / NEXT loops

  FOR variable = start TO finish STEP increment
    statements
  NEXT variable

In pseudocode:

  • keywords are written in capitals

  • structural elements come in pairs, eg for every BEGIN there is an END, for every IF there is an ENDIF. •

  • indenting is used to identify control structures in the algorithm

  • the names of subprograms are underlined. This means that when refining the solution to a problem, a subroutine can be referred to in an algorithm by underlining its name, and a separate subprogram developed to show the logic of that routine. This feature enables the use of the top-down development concept, where details for a particular process need only be considered within the relevant subroutine.


Comments

comments powered by Disqus