36 of 158
30 The logic of the For loop in the example above may be exactly duplicated using other statements as illustrated below. You can see that the For loop allows you to express the same logic more concisely. Dim i as Integer i = 1 Do While (i <= 10) Debug.Print CStr(i) i = i + 1 Loop There are several things to note about the For-Next loop. Firstly, it is important to be aware that although the values of the <end-expr> and <step-expr> elements are used multiple times, the expressions are only evaluated once, when the execution of the For-Next begins. This distinction is only important, of course, if these expressions contain references to other variables that might be modified during loop execution or if they involve function calls. Secondly, because the loop entry test checks to see if the loop index variable is less than or equal to the <end-expr> you must choose a data type for the loop index variable that is capable of representing a value that is greater than the value of the <end-expr> . Otherwise, the loop entry test will always be true. Consider this errant example: Dim i as Byte For i = 0 to 255 <statements> Next i This loop will never terminate because the value of i is always less than or equal to 255 since it is a Byte type. Where possible, the compiler will issue a warning if it detects these problematic conditions. Even so, you should develop the habit of considering this potential problem every time you code a For- Next loop. 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. The DownTo keyword is not supported. 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