1. 程式人生 > >C 語言 switch 語句

C 語言 switch 語句

    在有多個選擇分支條件的情況下,用if 語句難免會繁瑣且效率不高,此時便是switch語言的用武之地。

     int  i=1;

     switch(i)

{

    case 0:

             printf("0");

    case 1:
            printf("1");

    case 2:

           printf("2");

    default:

           printf("default");

}

     有些人會認為執行的結果是 2, 但是實際情況是  12default.

     這也是初學者常犯的錯誤。按照常理,switch是選擇分支,即滿足那個case 執行那個case 塊的語句,但是C語言中的switch有它的個性。來看看MSDN 的定義。

     You can use the break statement  to end processing of a particular case within the switch statement and to branch to the end of the switch statement. Without break,the program continues to the next case, executing the statements until a break or the end of the statement is reached. In some situations, this continuation may be desirable。

     意思是,用break 語句 終止 當前的case ,直接到達switch 的末尾,執行完當前case 若有break則跳出switch。

     如果沒有break, 程式繼續流向下一個case ,直到遇到break,或者 到達了switch末尾。

     所以如果沒有break語句,找到匹配條件的case 後,會從這裡繼續執行下面的case 直到最後一個case 或default。

     明白了這個我們在上面的程式碼應該這樣寫。

        int  i=1;

     switch(i)

{

    case 0:

             printf("0");

            break;

    case 1:
            printf("1");

           break;

    case 2:

           printf("2");

          break;

    default:

           printf("default");

          break;

}

     這樣就會輸出  1;

     一定要養成良好的習慣,給每一個case 加上break,以免造成疏忽的錯誤。

     但是C語言的這種switch 特性也有它的優點。

     比如設定每一個月的天數 Days 可以這樣用switch

     switch(month)

 {

     case 1:

     case 3:

      case 5:

      case 7:

     case 8:
     case 10:

      case 12   :

                  Days=31;

                  break;

     case 2:

                //根據是否是閏年判斷。

               break;

      case 4:

       case 6:

      case 9:

        case 11:

                     Days=30;

                    break;

     default:    break;

}

    這樣便不用給每個case 都寫語句了,case 1 3 5  7 為空,會自動到下一個CASE ,知道case 12 ,執行完跳出。

   關於 defaut的位置。

    這個問題相信有一大部分人沒有考慮過,因為常見的程式中default 都在最後,這要造成了一些人認為default 在最後。

    MSDN 的說法:

    There can be at most one default statement. The default statement need not come at the end; it can appear anywhere in the body of the switch statement. In fact it is often more efficient if it appears at the beginning of the switch statement. A case or default label can only appear inside a switch statement. 

   最多有一個default 語句在一個switch 中,default 宣告不一定在末尾,它可以出現在switch的任何地方那個,實際上如果default出現在switch 的開頭會更有效率。

   所以

   int i=2;

   switch(i)

   {

       default :break;

        case 0:

                  printf("0");

                 break;

        case 1:

                 printf("0");

                 break;

        case 2:

               printf(“2”);

              break;

   }    也是合理的。

     關於switch()引數值的型別

     引數值型別必須是這幾種型別之一:int,long,short ,byte,char.

    switch為什麼只能用int,short,byte,char,long,因為switch 只能使用 int 型別或者是可以轉換為 int型別的引數(char,char 和 int 通過ascii轉換)。

     C語言沒有規定一定是int 型別,但是要求是完整的,因此只能是上面幾種了。

      關於switch 中case 的個數。

     標準的C編譯器至少允許一個switch中最多257個case 標籤。這是為了允許switch滿足一個字元8位的所有情況,總共是2的8次方256,再加上EOF.

     而Miscrosoft C 編譯器的switch中case 是這樣定義的。

      MSDN :

     Microsoft C does not limit the number of case values in aswitch statement. The number is limited only by the available memory. ANSI C requires at least 257 case labels be allowed in aswitch statement.

     它不限制case 的個數。 only limited by available memory.

    關於switch 中宣告變數初始化的問題。

     int i=2;

     switch(i)

     {

        case 1:

                    break;

        case 2:

                   int sum=5;

                   sum=sum*i;

                   printf("%d",sum);

                  break;

         default:

                 break;

    }

   執行的結果是什麼呢。有人可能認為是  10,然而這樣寫編譯器會報錯,在VC6,下 為  error C2361: initialization of 'sum' is skipped by 'default' label

  意思是sum變數的初始化被default 跳過 ,沒有被初始化。

   來看MSDN 的說明:

   Note   Declarations can appear at the head of the compound statement forming theswitch body, but initializations included in the declarations are not performed. Theswitch statement transfers control directly to an executable statement within the body, bypassing the lines that contain initializations.

   宣告可以出現在switch 塊中複合語句的頭部(這是C語言的規則,不允許在語句的中間宣告變數,C++可以 ,當然在VC6下可以的通過的,因為它是C/C++的混合編譯器),但是包含在宣告中的初始化不會被執行。

    當寫成 int sum;   不報錯,執行結果為  -858993460,隨機的,因為沒有初始化。

    解決的方法為給case 下的語句加上{} 號,便可初始化。

        int i=2;

     switch(i)

     {

        case 1:

                    break;

        case 2:

                 {

                   int sum=5;

                   sum=sum*i;

                  printf("%d",sum);

                  break;

                }

         default:

                 break;

    }

     執行結果為10,已初始化。