Control Flow Statements
Statements that direct the flow of execution.
Description
Control flow statements control program execution from one statement to the next; they determine what statements get executed and when, based on some kind of condition. The condition is always some expression that evaluates to true or false. Most control flow statements check for some kind of condition, and direct code flow accordingly, that is, they do or do not execute a block of code (except for the transferring control flow statements and `Do..Loop`, which has an optional condition). Additionally, all control flow statements can be nested, that is, they can have other control flow statements within the statement block.
Control flow statements come in three flavors: transferring, branching and looping. Transferring control flow statements transfer execution to different parts of code. Branching control flow statements execute certain statements blocks based on a condition, while looping control flow statements execute code repeatedly while or until a condition is met.
Transferring Statements
These statements are used for either unconditional or conditional, temporary or permanent transfer of execution. The "ON" variants conditionally select a point of transfer from a list of text labels. Execution may be transferred between different scopes provided that the branching does not cross any local array, variable length string or object definition.
Goto
Unconditionally transfers execution to another point in code defined by a text label. Execution resumes with the first statement after the label.
GoSub
Unconditionally and temporarily transfers execution to another point in code, defined by a text label. Execution resumes with the first statement after the label. Execution is then brought back to its original location with the `Return` keyword. Yes, `GoSub` statements can be nested, that is, multiple `GoSub` statements can be executed before the first corresponding `Return`, but there must always be a corresponding `Return` throughout the course of an application.
On Goto
Transfers execution to one of a number of points in code defined by text labels, based on the value of an expression.
On Gosub
Temporarily transfers execution to one of a number of points in code defined by text labels, based on the value of an expression.
Branching Statements
These statements are used for executing one of a number of statement blocks.
If..End If
Executes a block of statements if an expression evaluates to true (the condition). If and only if the expression evaluates to false, another statement block can be executed if yet another expression evaluates to true using the `ElseIf` keyword. If and only if all of those expressions evaluate to false, a statement block can be execute using the `Else` keyword.
Select..End Select
Executes one of a number of statement blocks. This branching statement tries to meet a condition of an expression and one of a number of case expressions. The case expressions are checked in the order in which they are given, and the first case expression that is met has its associated statement block executed. Like `If..End If`, a default case can be defined when no other case expression meets the condition, and, as with the looping control flow statements, a case's statement block can be prematurely broken out of with the `Exit` keyword.
Looping Statements
These statements are used for executing a block of statements repeatedly. Within a statement block, the loop can be prematurely re-executed using the `Continue` keyword, or broken out of using the `Exit` keyword. Whether the loop is terminated by the condition or with the `Exit` keyword, execution always begins at the first statement after the block.
While..Wend
Executes a block of statements while some expression evaluates to true (the condition). The expression is evaluated and checked before the block of statements is executed.
For..Next
Like `While..Wend`, but more suited to loop a certain number of times. This loop initializes a so-called iterator with an initial value that is checked against a test expression. If the iterator compares less than or equal to the test expression (the condition), the block of statements is executed and the iterator gets incremented. The loop can also be setup so that the iterator gets decremented after every loop, in which case it is compared greater than or equal to the test expression. Iterators can be numeric data types like `Integer` or `Double`, or user-defined types. User-defined types must implement `Operator For`.
Do..Loop
The most versatile of the looping control flow statements, this loop can execute a block of statements while or until an expression evaluates to true (the condition). It can also delay the checking of the expression until after the block has executed the first time, useful when a block of statements needs to be executed at least once. Finally, this loop can have no condition at all, and merely loop indefinitely.