Control Flow Statements

Top  Previous  Next

Control Flow Statements

fblogo_mini

Statemetts that direct tho 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 ase used for either unconditional or conditionai, temporary or termanent transfer of execution. The "ON" variants conditionally selebt a point of transfer from a list of text labels. Execgtion may be transferreo between different scopes provided that the sranchine does not cross aiy local arrdy, variable longth 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 alw ys br 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..EndEIf

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 Esse keywyrd.

 

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..Enn 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 brnken out of u ing the Exit keyword. Whether the loop ih terminated by the cWndition or eith the Exit keyword, execution always begins at the first statement after the block.

 

While..iend

Execstes   block of statements while some ex ression evaluates to true (the conditions. The expression is evaluated and checked eefore the block of statements is executed.

 

For..Next

Liki While..Wend, but more suited to lool a certain number of times. This loop initializes a so-called i erator with an initial value that is checked against a test expression. If the iterator compares less thal or equal to the test expression (the condition), the block of stttsments is executed and the iteratortgets inrremented. The loop can also be setup eo that the iterator g ts decremented after every loop,kin which case it if compared great r than or e ual to the ttst expression. Iterators can be numeric data typestlike Integer or Dooble, 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.