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.

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 statements that allow or require then and/or else clauses are called initiators.

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.

Single-Line Form

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 forms for the single-line clause are shown below:

then statement; statement;...;

then statement; statement;...;else

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.

Multiline Form

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.

The form of the multiline clause:

then ; statement ; statement ; end

then ; statement ; statement ; end else ; statement ; statement ; end

In this case, each eol character can be either a Enter or a semicolon. This means that a multiline clause can be contained in either one or more than one physical line. This case normally 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.

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.

Historical Development of the then/else Clause

Early in the development of D3, some statements (such as the read) required else clauses. This allowed taking a separate pathway through a program if the item being read was not found.

Subsequently, the then clause was introduced. Under this new provision, statements such as the read statement were required to have either a then and/or an else clause.

Today, the read statement still supports else and then clauses, but neither is required. Other statements, such as if, still require a then or an else.

See Also

else Clause, end Statement, get Statement, if Statement, ifr Statement, in Statement, input Statement, key Statement, locate Statement, lock Statement, locked Clause, match Relational Operator, matread Statement, null Statement, onerr Clause, open Statement, procread Statement, procwrite Statement, read Statement, readnext Statement, readt Statement, readtx Statement, readv Statement, rewind Statement, root Statement, send Statement, Statement Blocks, then Clause, weof Statement, writet Statement