C switch

2018-05-19 18:26 更新

學(xué)習(xí)C - C switch

switch..case可以聲明如下:

   switch(option){ 
      case option1: 
              // do option1 job 
              break; 
      case option2: 
              // do option2 job 
              break; 
   } 

switch語句使您能夠根據(jù)整數(shù)表達(dá)式的結(jié)果從一個(gè)操作列表中選擇一個(gè)操作。

switch語句的一般語法如下:

switch(integer_expression)
{
  case constant_value_1:
    statements_1;
    break;
    ....
  case constant_value_n:
    statements_n;
    break;
  default:
    statements;
    break;
}

如果integer_expression對(duì)應(yīng)于由關(guān)聯(lián)的constant_value_n值定義的一個(gè)case值,那么執(zhí)行該case值之后的語句。

如果integer_expression的值與每個(gè)case值不同,則默認(rèn)執(zhí)行的語句將被執(zhí)行。

您可以省略默認(rèn)關(guān)鍵字及其關(guān)聯(lián)的語句。

例子

以下是switch..case用法的示例代碼:


   #include <stdio.h> 

   int main() { 
      // you can obtain input value from keyboard 
      // or any input device 
      int input = 3; 

      switch(input){ 
      case 1: 
              printf("choosen 1\n"); 
              break; 
      case 2: 
              printf("choosen 2\n"); 
              break; 
      case 3: 
      case 4 : 
              printf("choosen 3\n"); 
              break; 
      } 
      return 0; 
   } 

上面的代碼生成以下結(jié)果。



例2

例子


    #include <stdio.h>

    int main(void)
    {
      int choice = 0;            // The number chosen

      // Get the choice input
      printf("Pick a number between 1 and 10! ");
      scanf("%d", &choice);

      // Check for an invalid selection
      if((choice > 10) || (choice < 1))
        choice = 11;             // Selects invalid choice message

      switch(choice)
      {
        case 7:
          printf("777!\n");
          break;                 // Jumps to the end of the block

        case 2:
          printf("222.\n");
          break;                 // Jumps to the end of the block

        case 8:
          printf("888.\n");
          break;                 // Jumps to the end of the block

        case 11:
          printf("Try between 1 and 10.\n");
                                 // No break - so continue with the next statement

        default:
          printf("Sorry, you lose.\n");
          break;                 // Defensive break - in case of new cases
      }
      return 0;
}

例子



例3

以下代碼使用switch語句來處理用戶輸入。


#include <stdio.h>

int main(void)
{
  char answer = 0;       // Stores an input character

  printf("Enter Y or N: ");
  scanf(" %c", &answer);

  switch(answer)
  {
    case "y": case "Y":
      printf("You responded in the affirmative.\n");
      break;

    case "n": case "N":
      printf("You responded in the negative.\n");
      break;
        default:
          printf("You did not respond correctly. . .\n");
          break;
      }
      return 0;
    }

上面的代碼生成以下結(jié)果。

例4

使用多個(gè)標(biāo)簽


#include <stdio.h>
int main(void)
{
    char ch;
    int a_ct, e_ct, i_ct, o_ct, u_ct;
    
    a_ct = e_ct = i_ct = o_ct = u_ct = 0;
    
    printf("Enter some text; enter # to quit.\n");
    while ((ch = getchar()) != "#")
    {
        switch (ch)
        {
            case "a" :
            case "A" :  a_ct++;
                break;
            case "e" :
            case "E" :  e_ct++;
                break;
            case "i" :
            case "I" :  i_ct++;
                break;
            case "o" :
            case "O" :  o_ct++;
                break;
            case "u" :
            case "U" :  u_ct++;
                break;
            default :   break;
        }                    // end of switch
    }                        // while loop end
    printf("number of vowels:   A    E    I    O    U\n");
    printf("                 %4d %4d %4d %4d %4d\n",
           a_ct, e_ct, i_ct, o_ct, u_ct);
    
    return 0;
}

上面的代碼生成以下結(jié)果。

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)