C语言教程:switch语句从多个条件执行代码

2018-10-0608:19:43编程语言入门到精通Comments9,379 views字数 1731阅读模式

C语言中的switch语句用于从多个条件执行代码。 就像if else-if语句一样。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6396.html

C语言switch语句的语法如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6396.html

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

C语言中switch语句的规则如下 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6396.html

  1. switch表达式必须是整数或字符类型。
  2. case值必须是整数或字符常量。
  3. case值只能在switch语句中使用。
  4. switch case中的break语句不是必须的。这是一个可选项。 如果在switch case中没有使用break语句,则匹配case值后将执行所有后的语句。它被称为通过C语言switch语句的状态。

我们试着通过例子来理解它。假设有以下变量及赋值。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6396.html

int x,y,z;  
char a,b;  
float f;
C
有效的Switch无效的Switch有效的Case无效的Case
switch(x)switch(f)case 3;case 2.5;
switch(x>y)switch(x+2.5)case ‘a’;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语句的流程图 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6396.html

我们来看一个简单的C语言switch语句示例。创建一个源文件:switch-statment.c,其代码如下 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6396.html

#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");
    }
}
C

执行上面示例代码,得到以下结果 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6396.html

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

执行第二次,结果如下 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6396.html

Enter a number:50
number is equal to 50
请按任意键继续. . .
Shell

switch语句直通到尾文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6396.html

在C语言中,switch语句是通过的,这意味着如果在switch case中不使用break语句,则匹配某个case之后的所有的case都将被执行。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6396.html

我们来试试通过下面的例子来了解switch语句的状态。创建一个源文件:switch-fall-through.c,其代码如下所示 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6396.html

#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");
    }
}
C

执行上面示例代码,得到以下结果 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6396.html

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
请按任意键继续. . .
Shell

从上面的输出结果中,可以清楚地看到,当匹配 number = 10 之后,由于没有break语句,其它后面的语句也打印执行了。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6396.html

文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6396.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/ymba/6396.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定