Page 63 - Programming Microcontrollers in C
P. 63

48     Chapter 1  Introduction to C

                          has the normal printf arguments. Note however that the string is
                          not confined to one line. C compilers will not allow a string to be
                          split among several lines in a program. However, an ANSI C compli­
                          ant compiler will cause two adjacent strings to be concatenated into
                          a single string, so the above code will compile without error.

            Break, continue, and goto

                              These commands will cause a C program to alter its program
                          flow. If a break statement is encountered, the program will exit the
                          loop in which it is executing. Break can be used to exit for, while,
                          do-while, and switch statements. An example of the use of the
                          break statement is shown in the next section.
                              The continue statement causes the next iteration of a for,
                          while, or a do while loop to be started. In the case of the for
                          statement, the last argument is executed, and control is passed to the
                          beginning of the for loop. For both the while and the do-while,
                          the argument of the while statement is tested immediately, and the
                          program proceeds according to the result of the test.
                              The break statement is seen frequently, and the continue
                          statement is rarely used. Another statement that is even more rarely
                          used is the goto. In C, the programmer can create a label at any
                          location by typing the label name followed by a colon. If it is neces­
                          sary, the goto <label> can be used to transfer control of the
                          program from one location to another. In general, C provides enough
                          structured language forms that the use of the goto <label> se­
                          quence will rarely be needed. One place where the goto can be
                          used effectively is when the program is nested deeply and an error is
                          detected. In such a case, the goto statement is an effective means of
                          unwinding the program from a deep loop to an outer loop to process
                          the error. In general, you should avoid goto statements whenever
                          possible.
                              That said, there is an excellent alternative to a goto. The reach
                          of a goto is limited to the function in which it is defined. In fact, the
                          reach should probably be confined to the block in which it is de­
                          fined. Since new variables can be defined at the beginning of any
                          block, undefined behavior can be introduced when a goto branches
                          into a block where new variables have been defined. Also, you can
                          introduce undefined behavior when you branch out of a block where
   58   59   60   61   62   63   64   65   66   67   68