版本1和2间的区别
于2006-06-18 20:11:29修订的的版本1
大小: 3327
编辑: czk
备注:
于2007-07-18 19:42:48修订的的版本2
大小: 3377
编辑: czk
备注:
删除的内容标记成这样。 加入的内容标记成这样。
行号 1: 行号 1:
## page was renamed from Control Flow/3.4 Switch

Navigation(slides)

3.4 Switch

The switch statement is a multi-way decision that tests whether an expression matches one of a number of constant integer values, and branches accordingly.

   switch (expression) {
       case const-expr: statements
       case const-expr: statements
       default: statements
   }

Each case is labeled by one or more integer-valued constants or constant expressions. If a case matches the expression value, execution starts at that case. All case expressions must be different. The case labeled default is executed if none of the other cases are satisfied. A default is optional; if it isn't there and if none of the cases match, no action at all takes place. Cases and the default clause can occur in any order.

In Chapter 1 we wrote a program to count the occurrences of each digit, white space, and all other characters, using a sequence of if ... else if ... else. Here is the same program with a switch:

   1    #include <stdio.h>
   2 
   3    main()  /* count digits, white space, others */
   4    {
   5        int c, i, nwhite, nother, ndigit[10];
   6 
   7        nwhite = nother = 0;
   8        for (i = 0; i < 10; i++)
   9            ndigit[i] = 0;
  10        while ((c = getchar()) != EOF) {
  11            switch (c) {
  12            case '0': case '1': case '2': case '3': case '4':
  13            case '5': case '6': case '7': case '8': case '9':
  14                ndigit[c-'0']++;
  15                break;
  16            case ' ':
  17            case '\n':
  18            case '\t':
  19                nwhite++;
  20                break;
  21            default:
  22                nother++;
  23                break;
  24            }
  25        }
  26        printf("digits =");
  27        for (i = 0; i < 10; i++)
  28            printf(" %d", ndigit[i]);
  29        printf(", white space = %d, other = %d\n",
  30            nwhite, nother);
  31        return 0;
  32    }

The break statement causes an immediate exit from the switch. Because cases serve just as labels, after the code for one case is done, execution falls through to the next unless you take explicit action to escape. break and return are the most common ways to leave a switch. A break statement can also be used to force an immediate exit from while, for, and do loops, as will be discussed later in this chapter.

Falling through cases is a mixed blessing. On the positive side, it allows several cases to be attached to a single action, as with the digits in this example. But it also implies that normally each case must end with a break to prevent falling through to the next. Falling through from one case to another is not robust, being prone to disintegration when the program is modified. With the exception of multiple labels for a single computation, fall-throughs should be used sparingly, and commented.

As a matter of good form, put a break after the last case (the default here) even though it's logically unnecessary. Some day when another case gets added at the end, this bit of defensive programming will save you.

Exercise 3-2. Write a function escape(s,t) that converts characters like newline and tab into visible escape sequences like \n and \t as it copies the string t to s. Use a switch. Write a function for the other direction as well, converting escape sequences into the real characters.

Navigation(siblings)

TCPL/3.4_Switch (2008-02-23 15:34:13由localhost编辑)

ch3n2k.com | Copyright (c) 2004-2020 czk.