Start Back Next End
  
ZBasic Language Reference
38
ZBasic Microcontrollers
BasicX Compatibility Note
In BasicX mode, the For-Next statement is much more restrictive.  The loop index variable must be
a scalar integral type and must also be a local variable.  Referring to the loop index variable in a
Next statement is not supported.  The <step-expr> is restricted to a constant expression that
evaluates at compile time to either 1 or –1. Lastly, For-Next loops may be nested to a maximum
depth of 10 for compatibility.
When Option Strict is enabled, there are additional restrictions that apply.  Firstly, the loop index
variable must be local to the routine; it cannot be defined at the module level.  Secondly, the loop index
variable is not allowed to be used or modified outside of the For-Next loop except that it can be used as
the loop index variable in a subsequent For-Next loop.  Thirdly, inside the For-Next loop the loop index
variable is read-only.  Any attempt to modify the loop index variable, or pass it by reference to another
routine will result in an error message from the compiler.
One final note: although loop index variables of type Single are allowed, some experienced
programmers advise against doing so.  This is due to the fact that not all real numbers can be exactly
represented as a Single value.  Consequently, using a Single loop index variable may not produce the
expected results.  It is often better to use an integral loop index variable along with an auxiliary real
variable to accomplish the desired objective.
2.5.9 Goto Statement
The Goto statement allows you to transfer control to a specific point in the sequence of statements that
comprise a subroutine or function.  The point to which control is transferred is marked by a label
statement.  The label statement is simply an identifier followed by a colon appearing on a line by itself
(except that it may be followed by a comment).
Because it interferes with the normal program flow, the Goto statement can be overused resulting in a
program that is difficult to understand and, therefore, difficult to maintain.  Some programmers believe
that a Goto statement should never be used.  Others believe that it is acceptable to use a Goto but only if
the alternative code structure is even less palatable.  The latter strategy is probably the best to adopt.
Example
    Goto doOtherStuff
    <other-statements>
doOtherStuff:
    <other-statements>
2.5.10 If-Then-Else Statement
The if-then-else compound statement is the basic decision making construct in ZBasic.  In its simplest
form, the syntax is:
If <boolean-expression> Then
    <statements>
End If
The <boolean-expression> element is an expression whose value is of type Boolean.  It most often
involves one of the conditional operators that allow you to compare the values of two expressions but it
may also simply be the invocation of a function whose return type is Boolean.
The <statements> element represents zero or more ZBasic statements possibly including other If
statements.  This allows you to create nested decision-making statements of arbitrary complexity.
Previous page Top Next page