7.2.6 Flow Control with IF - THEN - ELSE

In 7.2.4 we have seen the assignment statement

LET output = (i>5)

An assignment is an expression with a value and assignments associate from right to left. The condition test (i>5) returns the value 1 or 0, depending whether the test results true or false. But CSI(C&F) offers the possibility, to formulate more transparent and complex decision structures. Now we modify the referenced example to bring it to a more readable form:

/* the use of IF - THEN - ELSE */

FOR i = 1 TO 10 STEP 1

IF (i>5) THEN

PRINT i

PRINT " is greater than 5"

PRINT CR

ELSE

PRINT i

PRINT " is lower than or equal 5"

PRINT CR

ENDIF

NEXT i

This example shows a THEN and an ELSE statement block, which specifies the alternative actions if the condition part of an IF statement is true or false. The general form is

IF <condition> THEN

<statement block 1>

ELSE

<statement block 2>

ENDIF

One and only one of the two statement blocks associated with an IF-THEN-ELSE statement is performed. If the condition is true, <statement block 1> is executed; if not, <statement block 2> is executed. Like anywhere, a statement block can be a single statement or several statements.