SELECT structure

An algorithm may contain a series of decisions in which a variable or an expression is separately tested for each of a set of potential values, and a distinct instructions sequence is consequently executed. LARP offers the SELECT structure for implementing such algorithmic structure.

The SELECT structure replaces the IF-ELSE-IF structure while making the algorithm more readable. Consider the following pseudo code which uses an IF-ELSE-IF structure:

 READ Value1, Value2, Operator  IF Operator = '+' THEN    Result = Value1 + Value2  ELSE IF Operator = '-' THEN    Result = Value1 - Value2  ELSE IF Operator = '*' OR Operator = 'x' THEN    Result = Value1 * Value2  ELSE IF Operator = '/' THEN    Result = Value1 / Value2  ELSE    WRITE "Bad operator"  ENDIF  WRITE Result

Now here is an equivalent pseudo code using a SELECT structure (using reserved words SELECT, ELSE and ENDSELECT):

 READ Value1, Value2, Operator  SELECT Operator    '+'      : Result = Value1 + Value2    '-'      : Result = Value1 - Value2    '*', 'x' : Result = Value1 * Value2    '/'      : Result = Value1 / Value2    ELSE       Result = "Bad operator"  ENDSELECT  WRITE Result

In the latter pseudo code the value of variable Operator is compared to each enumerated constant thereafter. If a constant corresponds to the value in Operator, the instructions sequence associated to this constant is executed.

If the value in Operator does not correspond to the first listed constant, it is compared with the following one, and so on. The ELSE section allows the algorithm to take appropriate measures when the tested variable (Operator) corresponds to none of the listed constants.

Note that

• the tested expression may only be compared with constants (ex: '+ '),
• more than one constant may be associated with the same instructions sequence (ex: '* ', ' x '), and
• the ELSE section is optional.

The flowchart SELECT structure is similar to its IF-ELSE-IF counterpart, except for the tested expression which must be inserted at the entry point of the structure:

Here is the general form of a SELECT structure:

 SELECT structure SELECT expression     List of constants #1 :  Instructions sequence #1     List of constants #2 :  Instructions sequence #2     List of constants #3 :  Instructions sequence #3     ...     List of constants #n :  Instructions sequence #n     ELSE         Instructions sequence #n+1 ENDSELECT

where

• List of constants consists of one or more constants, separated by commas; do not forget the colon (:) at the end of the list in pseudo code.
• Each Instructions sequence may contain any number of LARP instructions. In pseudo-code the first instruction may be positioned after the : symbol, on the same line as the corresponding List of constants.
• The ELSE section with its instructions sequence are optional. In flowcharts the Instructions sequence #n+1 is also optional.

When executing a SELECT structure, LARP successively compares the value of expression with constants found in each List of constants, starting with the first list (List of constant #1). When a constant corresponding to the value of expression is found, its associated Instructions sequence is executed and the flow of execution leaves the SELECT structure afterwards. If no constant corresponding to the value of expression is found and the structure has an ELSE section, the corresponding instructions (Instructions sequence #n+1) is executed.

If more than one List of constants hold a constant corresponding to the value of expression, only the Instructions sequence corresponding to the first List of constants is executed since the flow of execution leaves the SELECT structure afterwards.

As with the IF-ELSE-IF structure, SELECT structures are inserted into flowcharts using the graphical editor with two flowchart instructions:

 Instructions Descriptions SELECT 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 mathematical expression. Branching for conditional structures:  allows to add alternate sequences of instructions in SELECT structure and IF-ELSE-IF conditional structure.