在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ C/ C語言switch語句
C語言printf()和scanf()函數(shù)
C語言strlen()函數(shù)
C語言指針
C語言ftell()函數(shù)
C語言#ifdef指令
C語言程序執(zhí)行流程
C語言歷史
C語言fprintf()和fscanf()函數(shù)
C語言#define指令
C語言#if指令
C語言將數(shù)組傳遞給函數(shù)
C語言指針的指針
C語言rewind()函數(shù)
C語言常量
C語言strcat()函數(shù)
C語言#ifndef指令
C語言continue語句
C語言注釋
C語言#include指令
C語言類型轉換
C語言strcpy()函數(shù)
C語言strlwr()函數(shù)
C語言while循環(huán)
C語言字符串
C語言strrev()函數(shù)
C語言gets()和puts()函數(shù)
C語言文件處理
C語言存儲分類
C語言運算符
C語言數(shù)據(jù)類型
C語言strcmp()函數(shù)
C語言VS開發(fā)環(huán)境安裝
C語言轉義序列
C語言第一個程序
C語言變量
C語言goto語句
C語言預處理器指令
C語言指針算術運算
C語言數(shù)學函數(shù)
C語言二維數(shù)組
C語言for循環(huán)
C語言命令行參數(shù)
C語言通過值和引用函數(shù)
C語言fputs()和fgets()函數(shù)
C語言do-while循環(huán)
C語言結構體數(shù)組
C語言循環(huán)
C語言#pragma指令
C語言關鍵字
C語言#error指令
C語言聯(lián)合體
C語言特點
C語言break語句
C語言遞歸
C語言函數(shù)
C語言結構體
C語言switch語句
C語言結構體嵌套
C語言fputc()和fgetc()函數(shù)
C語言fseek()函數(shù)
C語言字符串函數(shù)
C語言if-else語句
C語言教程
C語言宏
C語言數(shù)組
C語言strupr()函數(shù)
C語言#undef指令

C語言switch語句

C語言中的switch語句用于從多個條件執(zhí)行代碼。 就像if else-if語句一樣。

C語言中switch語句的語法如下:

switch(expression){    
case value1:    
 //code to be executed;    
 break;  //optional  
case value2:    
 //code to be executed;    
 break;  //optional  
......    

default:     
 code to be executed if all cases are not matched;    
}

C語言中switch語句的規(guī)則如下 -

  1. switch表達式必須是整數(shù)或字符類型。
  2. case值必須是整數(shù)或字符常量。
  3. case值只能在switch語句中使用。
  4. switch case中的break語句不是必須的。這是一個可選項。 如果在switch case中沒有使用break語句,則匹配case值后將執(zhí)行所有后的語句。它被稱為通過C語言switch語句的狀態(tài)。

我們試著通過例子來理解它。假設有以下變量及賦值。

int x,y,z;  
char a,b;  
float f;
有效的Switch 無效的Switch 有效的Case 無效的Case
switch(x) switch(f) case 3; case 2.5;
switch(x>y) switch(x+2.5) case ‘a(chǎn)’; case x;
switch(a+b-2) case 1+2; case x+2;
switch(func(x,y)) case ‘x’>’y’; case 1,2,3;

C語言中的switch語句的流程圖 -

我們來看一個簡單的C語言switch語句示例。創(chuàng)建一個源文件:switch-statment.c,其代碼如下 -

#include<stdio.h>  
#include<conio.h>  
void main() {
    int number = 0;

    printf("Enter a number:");
    scanf("%d", &number);

    switch (number) {
    case 10:
        printf("number is equals to 10\n");
        break;
    case 50:
        printf("number is equal to 50\n");
        break;
    case 100:
        printf("number is equal to 100\n");
        break;
    default:
        printf("number is not equal to 10, 50 or 100\n");
    }
}

執(zhí)行上面示例代碼,得到以下結果 -

Enter a number:88
number is not equal to 10, 50 or 100

執(zhí)行第二次,結果如下 -

Enter a number:50
number is equal to 50
請按任意鍵繼續(xù). . .

switch語句直通到尾

在C語言中,switch語句是通過的,這意味著如果在switch case中不使用break語句,則匹配某個case之后的所有的case都將被執(zhí)行。

我們來試試通過下面的例子來了解switch語句的狀態(tài)。創(chuàng)建一個源文件:switch-fall-through.c,其代碼如下所示 -

#include<stdio.h>  
#include<conio.h>  
void main() {
    int number = 0;

    printf("enter a number:");
    scanf("%d", &number);

    switch (number) {
    case 10:
        printf("number is equals to 10\n");
    case 50:
        printf("number is equal to 50\n");
    case 100:
        printf("number is equal to 100\n");
    default:
        printf("number is not equal to 10, 50 or 100\n");
    }
}

執(zhí)行上面示例代碼,得到以下結果 -

enter a number:10
number is equals to 10
number is equal to 50
number is equal to 100
number is not equal to 10, 50 or 100
請按任意鍵繼續(xù). . .

從上面的輸出結果中,可以清楚地看到,當匹配 number = 10 之后,由于沒有break語句,其它后面的語句也打印執(zhí)行了。


上一篇:C語言for循環(huán)下一篇:C語言變量