Control Structures Overview

The statements described in this section alter the sequential execution of instructions within a procedure, i.e. they alter the flow of control.  For example, these statements conditionally execute blocks of statements, repeatedly execute blocks of statements a fixed number of times or repeatedly execute blocks of statements until a condition is satisfied.

GoTo Statements

This instruction executes an unconditional branch and continues execution at a specified labeled statement.

GoTo label

A label must either conform to the conventions for a variable name (e.g. label3) or an integer literal (e.g. 1000).   To label an instruction, the label is placed first on the line followed by a colon (:) followed by any standard instruction.

In general, GoTo instructions can make programs more difficult to understand.  So, whenever possible, other control structures should be used in place of GoTo’s.

If…Then…Else…End If Statements

This control structure tests one or more expressions and conditionally executes at most one block of statements.

If condition Then
if_statements
ElseIf elseif_condition Then
elseif_statements
Else
else_statements
End If

This control structure first tests the condition to determine if it is True (<>0) or False (=0).  If True, the if_statements are executed and the remainder of the statements down to the End If are skipped.  If False, the if_statements are skipped and the first ElseIf or Else, if present, is processed.  If an ElseIf clause is present, its elseif_condition is tested and, if True, the associated elseif_statements are executed after which execution continues after the End If.  Otherwise, the elseif_statements are skipped and the next ElseIf or Else is processed.  If all conditional tests fail and an Else is present, the else_statements are executed.

An If…Then can contain several or no ElseIf clauses.  If present, these must be specified before the optional Else clause.

An If…Then can only contain a single optional Else clause.

Since True is defined to be <>0, any arithmetic expression that evaluates to <>0 will be interpreted as a True condition.

For simple tests, this statement can be reduced to a single line format: If…Thenstatement.

For…Next Statements

This control structure executes a sequence of instructions a fixed number of times.

For variable = initial_value To final_value Step increment
for_loop_statements
Next variable

This control structure begins by setting the variable to the initial_value.  The variable can be any numeric type, i.e.. Byte, Integer, Short, Single or Double.  Array variables as well as object and structure fields are also permitted.  However, object and structure properties are not permitted.

If the initial_value does not exceed the final_value, the for_loop_statements are executed once.  However, if the initial_value exceeds the final_value, the for_loop_statements are skipped and execution continues at the statement following the Next instruction.  If the for_loop_statements are executed, execution proceeds until the Next instruction is encounter.  When the Next statement is executed, the increment is added to the variable and its value is compared again to the final_value.  So long as the final_value is not exceeded, the for_loop_statements are executed again and the process is repeated.

The initial_value, final_value, and increment can all be arbitrarily complex arithmetic expressions.  However, these expressions are only evaluated when the For statement is executed and their values are saved for use by the Next statement.  Therefore, if the values of these expressions change during the execution of the For loop it does not alter the saved values.  Since these expression are only evaluated once, the For loop is generally more efficient that other looping methods.

The increment value is optional can be positive or negative.  If positive, looping terminates when the variable’s value is greater than the final_value.  If negative, looping terminates when the variable’s value is less than the final_value.  If not specified, a value of 1 is assumed.

The For loop can be prematurely terminated by executing an Exit For statement or a GoTo statement that branches outside of the For loop.

While…End While Statements

This control structure tests a condition and, if True, executes a block of statements repeatedly until the condition is False.

While test_expression
while_statements
End While

This control structure begins by evaluating the test_expression.  If the expression value is True (<>0), the block of while_statements is executed.  When the End While is encounter, the test_expression is evaluated again.  If the test_expression is still True, the while_statements are executed again.  This sequence is repeated so long as the test_expression remains True.  As soon as the test_expression tests False (=0), the while_statements are skipped and execution continues at the statement following the End While.

If the test_expression is False when the While begins execution, the while_statements are skipped and are not executed.

The While loop can be terminated before the conclusion of the while_statements by executing an Exit While statement or a GoTo statement that branches outside of the While loop.

Do…Loop Statements

This control structure bounds a block of instructions that are repeatedly executed so long as a specified expression evaluates to True or until the expression value becomes True.

Do While | Until condition
statements

Loop
         -or-
Do
statements
Loop While | Until condition

Select…Case…End Select Statements

This control structure executes one of several blocks of statements based upon matching a numeric or String expression value.  This control structure is similar to the If…Then...ElseIf statements in that a series of values are compared to determine the statements that are executed next.  However, this control structure is more efficient and convenient than a series of If statements if a single value is to be compared to multiple possible values.

Select match_value
Case test_expression,...,test_expression
case_statements

Case Else
else_statements
End Select

The match_value is evaluated once and then sequentially tested against each test_expression specified in a series of Case statements. When a matching test_expression value is found, the associated case_statements are executed.  Following the execution of the appropriate case_statements, execution continues at the statement following the End Select. If no test_expression is matched and a Case Else is present, the else_statements are executed.  If no test_expressionis is matched and a Case Else is not defined, none of the case_statements are executed and execution continues after the End Select.

The match_value can be a general numeric or String expression and can evaluate to any of the basic arithmetic data types (e.g. integer, real number, byte) or a String type.

Any number of Case statements can be included. Each Case statement can be followed by one or more numeric or String test_expression’s.

Each Case test_expression must take one of the following forms:

A general numeric expression, e.g. 2, a+b.

A general String expression, e.g. “blue”, stg1 & “ab”

If a test_expression does not match the data type of the match_value, the expression is automatically converted to the appropriate type.

Some example Case statements are as follows:

Case 1, 3, 5, 7, 11              ' First prime numbers 
Case "red", color2, "blue" & "green"

Executing an Exit Select instruction will skip the remaining statements within a group of case_statements or else_statements. Execution continues at the instruction following the End Select.

Nested Control Structures

In general, control structures can be nested within each other to an arbitrary depth and in arbitrary combinations.  For example, a While loop can be embedded within another While loop or an If…Then clause.