ZBasic Language Reference
36
ZBasic Microcontrollers
There is no fixed limit on how deeply Do loops may be nested. The actual limit is governed by how much
memory is available to the compiler. For all practical purposes, there is no limit.
BasicX Compatibility Note
In BasicX mode, the nesting of Do loops is limited to 10 for compatibility.
2.5.7 Exit Statement
The Exit statement allows you to terminate the execution of a loop, a function or a subroutine earlier than
it otherwise would. This is most commonly used when a condition is detected by the code that prevents
further normal processing. The syntax for the Exit statement is:
Exit <exit-type>
The <exit-type> element may be Do, For, Sub or Function but the use of each is restricted to use
within a Do-Loop, For-Next, subroutine and function, respectively. Any other use will result in a compiler
error.
Example
Do
<other-statements>
If (i > 5) Then
Exit Do
End If
<other-statements>
Loop
When an Exit Do is executed within nested Do-Loop statements only the innermost Do-Loop that
contains the exit statement will be terminated. Control will be transferred to the first statement following
the terminated Do-Loop. The same idea applies to an Exit For within nested For-Next statements.
2.5.8 For-Next Statement
The For-Next compound statement is another form of looping that provides controlled iteration using a
loop index variable. The syntax for a For-Next loop is:
For <var> = <start-expr> To <end-expr> [ Step <step-expr> ]
[<statements>]
Next [ <var> ]
The <var> element, referred to as the loop index variable, must refer to a previously defined scalar
variable (i.e. not an array element) that is either a numeric type or an enumeration type. The <start-
expr>, <end-expr> and optional <step-expr> elements must all be the same type as the loop index
variable.
Example
Dim i as Integer
For i = 1 To 10
Debug.Print CStr(i)
Next i
|