11.3 While-Wend statement

There are two versions of the While statement: A long and a short version.

1) Long version While statement:

 
While <expr> <loop-block> Wend

The While statement enters the loop if the given expression is true (non-zero). If the expression is false (zero) the loop will not be entered at all and execution will continue after the Wend statement. If While entered the loop it will repeat the loop as long as the given expression is true.

 
i = 0
While i < 100
    i = i + 1
Wend
DebugPrint(i)   ; prints 100

The loop above will be repeated until the expression i < 100 becomes false. This is the case when i is equal or greater to 100. Because we start from 0 and add 1 to i after each loop cycle, i has the value of 100 when the loop exits.

You may also want to have a look at the documentation of the Break and Continue statements. These can be used to exit from a loop or to jump to the end of it.

2) Short version While statement:

 
While <expr> Do <stat>

The short version behaves exactly like the long version but you do not have to include the Wend statement. The short While statement has the restriction that the loop block must only consist of one statement. If you need to execute multiple statements in the loop block, you have to use the long version. The identifier Do signals Hollywood that you want to use the short version.

The example from above could be written in the following way using the short While statement:

 
While i < 100 Do i = i + 1


Show TOC