Flow control

<< Click to Display Table of Contents >>

Flow control

SoftMaker Basic provides a number of statements that control the program flow in scripts. For example, there are statements that perform, skip or repeat statements depending on a condition. There are the following variations:

Goto branches

Goto Label1

.

.

.

Label1:

The Goto statement performs an unconditional jump the specified label – in the above example to the label "Label1".

Gosub branches

Gosub Label1

.

.

.

Label1:

 Statement(s)...

Return

A jump target must also be specified for the Gosub statement. The difference to the Goto statementis that the Gosub statement returns to the original program position as soon as it encounters a Return statement.

Do loops

With a Do Loop loop, a group of statements can be executed multiple times. There are the following variations:

Do While | Until Condition

 Statement(s)...

 [Exit Do]

 Statement(s)...

Loop

Or:

Do

 Statement(s)...

Loop While | Until Condition

The difference:

Do While and Do Until check the condition before beginning to execute the statements inside the loop. These will be executed only if the condition is true.

With Do ... Loop While and Do ... Loop Until, the condition is checked after the loop has been executed for the first time. This means that the statements inside the loop are carried out at least once.

While loops

While ... Wend loops are identical to Do While ... Loop loops. The condition is also checked before the first execution of the statements inside the loop.

While Condition

 Statement(s)...

Wend

For ... Next loops

A For Next loop repeats the statements it contains exactly n times using a counter. Each time the loop is run, this counter is incremented or decremented by the specified value. If you do not specify an increment, 1 is used as the increment.

For counter = StartValue To EndValue [Step Increment]

 Statement(s)...

Next

If branches

In an If Then block, statements are only executed if the specified condition is true. This condition must be an expression whose result is True or False (for example If a<b Then).

An If ... Then block can contain one or more lines. If it extends over multiple lines, it must be ended with an End If statement.

If Condition Then statement(s)... ' One-line syntax

Or:

If Condition Then ' Multiple-line syntax

 Statement(s)...

End If

A variation of this are If ... Then ... Else statements. Here, the statements after Else are executed if the condition is not true.

If Condition Then

 Statement(s)...

Else

 Statement(s)...

End If

Further branches can be achieved by chaining multiple If ... Then ... ElseIf statements together. However, this may lead to code that is hard to understand and it is therefore recommended to use the Select Case statement instead (see below).

If Condition Then

 Statement(s)...

ElseIf Condition Then

 Statement(s)...

Else

 Statement(s)...

End If

Select Case branches

In a Select Case statement, a variable is checked against multiple values.

Select Case Variable

 Case Value1

         Statement(s)...

 Case Value2

         Statement(s)...

 Case Value3

         Statement(s)...

 [Case Else

         Statements(s)...]

End Select

If the variable contains, for example, the value "Value1", the statements after Case Value1 will be executed. If it has none of the specified values, the code will jump to the statements after Case Else (if given; otherwise the structure will simply be exited from).