42 of 208
ZBasic Language Reference 34 ZX Microcontroller Family The third important aspect of the For-Next statement is that if the <step-expr> evaluates to a negative value, the sense of the loop entry test changes. In this case, the loop index variable is tested to see if it is greater than or equal to the <end-expr> and, if so, the statements of the loop are executed. Also, in this case the caveat noted above about the range of the loop index variable changes. The loop index variable must be capable of representing a value that is less than the value of the <end-expr>. When a For loop is used with an unsigned data type, the step value is considered to be negative if the most significant bit of the value is a 1. One other note: it is permissible for the <step-expr> to evaluate to zero. This will cause the For loop to execute indefinitely. The For-Next loop may be terminated at any time by using the Exit For statement. The presence of the <var> on the Next statement is optional. However, if it is present, it must match the name of the loop index variable of the For loop with which it is associated. There is no fixed limit on how deeply For-Next 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 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>