# 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
```

## See also

- [else clause](https://d3codex.com/pickbasic-flashbasic/else-clause/)
- [end statement](https://d3codex.com/pickbasic-flashbasic/end-statement/)
- [get statement](https://d3codex.com/pickbasic-flashbasic/get-statement/)
- [if statement](https://d3codex.com/pickbasic-flashbasic/if-statement/)
- [ifr statement](https://d3codex.com/pickbasic-flashbasic/ifr-statement/)
- [$chain directive](https://d3codex.com/pickbasic-flashbasic/dollar-chain-directive/)
- [input statement](https://d3codex.com/pickbasic-flashbasic/input-statement/)
- [key statement](https://d3codex.com/pickbasic-flashbasic/key-statement/)
- [locate statement](https://d3codex.com/pickbasic-flashbasic/locate-statement/)
- [lock statement](https://d3codex.com/pickbasic-flashbasic/lock-statement/)
- [locked clause](https://d3codex.com/pickbasic-flashbasic/locked-clause/)
- [match relational operator](https://d3codex.com/pickbasic-flashbasic/match-relational-operator/)
- [matread statement](https://d3codex.com/pickbasic-flashbasic/matread-statement/)
- [null statement](https://d3codex.com/pickbasic-flashbasic/null-statement/)
- [onerr clause](https://d3codex.com/pickbasic-flashbasic/onerr-clause/)
- [open statement](https://d3codex.com/pickbasic-flashbasic/open-statement/)
- [procread statement](https://d3codex.com/pickbasic-flashbasic/procread-statement/)
- [procwrite statement](https://d3codex.com/pickbasic-flashbasic/procwrite-statement/)
- [read statement](https://d3codex.com/pickbasic-flashbasic/read-statement/)
- [readnext statement](https://d3codex.com/pickbasic-flashbasic/readnext-statement/)
- [readt statement](https://d3codex.com/pickbasic-flashbasic/readt-statement/)
- [readtx statement](https://d3codex.com/pickbasic-flashbasic/readtx-statement/)
- [readv statement](https://d3codex.com/pickbasic-flashbasic/readv-statement/)
- [rewind statement](https://d3codex.com/pickbasic-flashbasic/rewind-statement/)
- [root statement](https://d3codex.com/pickbasic-flashbasic/root-statement/)
- [send statement](https://d3codex.com/pickbasic-flashbasic/send-statement/)
- [statement blocks](https://d3codex.com/pickbasic-flashbasic/statement-blocks/)
- [then clause](https://d3codex.com/pickbasic-flashbasic/then-clause/)
- [weof statement](https://d3codex.com/pickbasic-flashbasic/weof-statement/)
- [\](https://d3codex.com/pickbasic-flashbasic/writet-statement/)

---
Source: https://d3codex.com/pickbasic-flashbasic/then-else-statement-blocks/ - part of the D3Codex reference.
