Page 250 - Introduction to Microcontrollers Architecture, Programming, and Interfacing of The Motorola 68HC12
P. 250

8.4 Constants and Variables                                         227
































                                Figure 8.2. Case Statements

            The while statement of Figure 8.3a tests the condition before the loop is executed
        and is useful if, for example, a loop may have to be done 0 times. The do while
        statement (shown in Figure 8.3b) tests the condition after the loop is executed at least
        once, but it tests the result of the loop's activities. It is very useful in I/O software. It
        can similarly clear alpha[ 10]. Though perhaps less clear, it usually leads to more
        efficient code. The do while ( ) construct is generally more efficient than the while (}
        construct because the latter has an extra branch instruction to jump to its end.
            The more general for statement (shown in Figure 8.3c) has three expressions
        separated by semicolons (;). The first expression initializes variables used in the loop;
        the second tests for completion in the same style as the while statement; and the third
        updates the variables each time after the loop is executed. Any of the expressions in the
        for statement may be omitted. For example, for (i = 0; i < 10; i++) alpha[i]
        = 0; will clear the array alpha as the above loops did.
            The break statement will cause the for, while, or do while loop to terminate
        just as in the case statement and may be used in a conditional statement. For instance,
        for(;; ) {i++; if(i ™ 30) break;} executes the compound statement {i++;
        if (i ==30) break; } indefinitely, but the loop is terminated when i is 30.


        8.4 Constants and Variables

        An important feature of C, extensively used to access I/O devices, is its ability to
        describe variables and addresses of variables. If a is a variable, then &a is the address of
        a. If a is a variable that contains an address of another variable b, then* a is the
   245   246   247   248   249   250   251   252   253   254   255