Previous

Next

 

IF-ELSE-IF structure

 

As described in the previous section, embedded conditional structures reduce execution times since the flow of execution exits the structures as soon as one condition is satisfied and the execution of the corresponding sequence of instructions has completed.

The advantage of embedded conditional structures is however offset by the complexity of the algorithm when several conditions are involved. The following pseudo code is an example of embedded conditional structures making the algorithm difficult to understand:

 WRITE "Temperature of water? "
 READ Temp
 IF Temp <= 0 THEN
   WRITE "It's frozen"
 SINON
   IF Temp <= 12 THEN
     WRITE "It's cold"
   ELSE 
     IF Temp <= 25 THEN
       WRITE "It's warm"
     ELSE 
       IF Temp <= 75 THEN
         WRITE "It's hot"
       ELSE 
         IF Temp <= 100 THEN
           WRITE "It's very hot"
         ELSE 
           WRITE "It's burning"
         ENDIF
       ENDIF
     ENDIF
   ENDIF
 ENDIF

Note that such pseudo code is confusing. Authors often forget one or several  ENDIF when writing algorithms with such exceedingly embedded structures.

Deeply embedded conditional structures are also confusing in flowcharts. In the following flowchart the embedded structure is very large and cannot fit within the visual area of the graphical editor unless the view is zoomed out so far as to make the text unreadable on screen:

IF-ELSE-IF conditional structures simplify the use of embedded conditional structures in a context where the flow of execution must leave the structure as soon as a condition is satisfied and the corresponding sequence of instructions executed:

Structure conditionnelle SI-SINON-SI

SI condition #1 ALORS 
    Séquence d'instructions #1 
SINON SI condition #2 ALORS 
    Séquence d'instructions #2 
SINON SI condition #3 ALORS 
    Séquence d'instructions #3 
SINON SI ... 

    ... 

SINON SI condition #n ALORS 
    Séquence d'instructions #n 
SINON 
    Séquence d'instructions #n+1 
FINSI

This conditional structure is used when one and only one sequence of instructions must be executed when a corresponding condition is true. This structure can be interpreted as follow:

IF-ELSE-IF structures are inserted into flowcharts using two flowchart instructions:

Instructions

Description

IF-ELSE-IF conditional structure: conditional structure consisting of one or more sequences of instructions, one of which is to be executed according to the value of a given conditions.

Branching for conditional structures:  allows to add alternate sequences of instructions in SELECT structure and IF-ELSE-IF conditional structure.

Here is the previous example rewritten using an IF-ELSE-IF conditional structure:

 WRITE "Temperature of water? "
 READ Temp
 IF Temp <= 0 THEN
   WRITE "It's frozen"
 ELSE IF Temp <= 12 THEN
   WRITE "It's cold"
 ELSE IF Temp <= 25 THEN
   WRITE "It's warm"
 ELSE IF Temp <= 75 THEN
   WRITE "It's hot"
 ELSE IF Temp <= 100 THEN
   WRITE "It's very hot"
 ELSE
   WRITE "It's burning"
 ENDIF

In a IF-ELSE-IF pseudo code structure, the last sequence of instructions (ELSE Instructions sequence #n+1) is optional. It is also the case in flowchart IF-ELSE-IF structures.

While both forms of conditional structures (embedded IF-ELSE and IF-ELSE-IF) are logically equivalent, the latter is preferred because it avoids deep indentation of pseudo code to the right. Such indentation often leaves most of the pseudo code line empty, forces the continuation of lines (using $) and diminishes the legibility of the algorithm. The flowchart IF-ELSE-IF structure is also preferred to its embedded counterpart because it’s more linear.

How LARP distinguish between IF-ELSE-IF conditional structures from embedded conditional structures in pseudo code? When reserved words ELSE and IF follow each other on the same line, then LARP assumes that they are part of a IF-ELSE-IF structure.