36 of 208
ZBasic Language Reference 28 ZX Microcontroller Family changing the state of the hardware. Consider the evaluation of the conditional expression in the If statement below when the value of the variable a is, say, 10. If (a > 3) Or (foo() > 10) Then [other statements] End If When the expression on the left side of the Or operator is evaluated the result will be True. Because of this fact we know that the resulting value of the entire conditional expression will also be True – nothing on the right hand side can possibly affect the outcome. Nonetheless, the expression on the right hand side of the Or operation will still be evaluated and thus the function foo() will be invoked. Some other computer languages, notably C/C++ and Java, implement the concept of “short circuit evaluation”. In those languages, the evaluation of an expression stops as soon as the result is known. If that were the case here, the right hand side of the Or expression would not be evaluated and, hence, the function foo() would not be invoked. To reiterate, ZBasic does not implement short circuit evaluation. 2.5 Statements Within a subroutine or function you can define variables and use statements to implement the logic required for the functionality of the routine. This section describes the types of statements available. ZBasic statements may be divided into two general categories: simple and compound. An example of a simple statement is the assignment statement where the entire statement is expressed on one line (ignoring possible line continuations). In contrast, a compound statement comprises two or more lines and may contain other statements within it. In many respects, it is convenient to think of a compound statement as if it were a single statement even though it may have many constituent statements. 2.5.1 Assignment Statement The assignment statement is perhaps the most basic and most often used statement in a program. The syntax of an assignment statement is shown below in two forms, one for assigning a value to a scalar variable and one for assigning a value to an array element. <var-name> = <value> <var-name>( <index-list> ) = <value> In both cases, the <value> element may be a simple value or a complex expression involving one or more other variables, constants, functions, etc. However, the type of <value> must match the type of the variable or array element to which it is being assigned. There is no automatic type conversion. When assigning a value to an array element, you must specify the index or indices of the particular element of interest. For a single-dimensional array, you will specify a single value for the index of the desired element. For multi-dimensional arrays, you must specify a value for each of the indices separated from one another by a comma. Example Dim i as Integer Dim ia(1 to 5) as Integer Dim board(1 to 12, 1 to 12) as Byte i = 2 ia(i) = (ia(3) + 2) / 4 board(i, 6) = CByte(ia(2))