codecamp

C switch

学习C - C switch

switch..case可以声明如下:

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

switch语句使您能够根据整数表达式的结果从一个操作列表中选择一个操作。

switch语句的一般语法如下:

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

如果integer_expression对应于由关联的constant_value_n值定义的一个case值,那么执行该case值之后的语句。

如果integer_expression的值与每个case值不同,则默认执行的语句将被执行。

您可以省略默认关键字及其关联的语句。

例子

以下是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; 
   } 

上面的代码生成以下结果。



例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;
    }

上面的代码生成以下结果。

例4

使用多个标签


#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;
}

上面的代码生成以下结果。

C If
C for
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

C 联合

C 预处理

C 索引

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }