then/else statement blocks

then/else statement blocks are found in conjunction with conditional statements.

Syntax

then statement.block
else statement.block
then statement.block else statement.block

Description

The most notable case is the if statement, which requires either a then or an else clause.

The statements that allow or require then and/or else clauses are called initiators. In all statements that allow or require then and/or else:

  • The then branch is taken if the operation is performed successfully or a true is returned.
  • The else clause is taken when the operation is unsuccessful or false is returned.

The possible syntactic structures are:

Structure Example
initiator then statements else statements if ans = "y" then crt "yes" else crt "not yes"
initiator then statements if ans = "y" then crt "yes"
initiator else statements if ans = "y" else crt "not yes"

When both then and else clauses are present, the then clause can be considered to be the initiator of the else clause. All other characteristics are identical, except that an else clause can succeed a then clause.

Within the then clause, the then token is the initiator, which requires a terminator. The clause can exist on one or more than one line. The nature of the terminator varies between these cases.

NULL statements are often included in then or else clauses as placeholders. A NULL statement performs no action. However, you can use NULL statements to make the logic of a conditional more clear and easier to read.

Note: This construction is not required. The then clause is not mandatory in an if statement, as long as an else clause exists.

If the then clause is omitted, the statement would make sense to the compiler, but it might not be clear to the user. The NULL statement is used to make clear to the user that if the match returns a value of true, no action is taken.

Single-line format

If the then clause is complete in one physical line, its terminator is an Enter or an else token.

initiator then statements
initiator then statements else statements
initiator else statements

The syntax of the else clause is the same as that of the then clause, except that it cannot be followed by another else clause.

Multi-line format

If the then clause spans more than one physical line, the then token must immediately be followed by an end-of-line (eol) character. This can be either an Enter or a ;. In this case, the clause is terminated by end preceded by an eol character.

initiator then
     statements
     statements
     statements
end

There must be one or more statements between the then and its terminator. If there are several statements, they must be separated by eol characters. If the then clause is the one-physical-line clause, the eol character must be a semicolon. If the then clause spans more than one physical line, the eol characters can be either an Enter, a semicolon, or both.

Multiple end statements

Multiline statements can have more than one end statement. The end statement then becomes the initiator for the else condition as follows:

initiator then
statement
statement
end else
statement
statement
end

Multiple end else sequences can be used, provided that each ends with an end statement.

Example(s)

Example 1

The forms for the single-line clause are shown as follows:
then statement; statement;...;
then statement; statement;...;else

Example 2

The following is an example of the form of the multi-line clause:
then ; statement; statement; end
then ; statement; statement; end else ; statement; statement; end

Example 3

In this case, each eol character can be either a Enter or a semicolon. This means that a multi-line clause can be contained in either one or more than one physical line. This case typically displays as:
then
statement
statement
end

For program clarity, the statements are indented from the beginning of the initiator, and the then or end else are aligned with the beginning of the initiator.

Example 4

The following is an example testing if a value is numeric:
IF NUM(PRICE) THEN
           NULL
END ELSE
     PRINT "ERROR: NON-NUMERIC PRICE. STOP"
STOP
END