首页>新闻>昆明有名的C++技术培训机构学费多少

昆明有名的C++技术培训机构学费多少

来源:达内IT教育-昆明校区

时间:2019/10/29 17:32:20

C语言switch语句

C语言中的switch语句用于从多个条件执行代码。就像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

C语言中switch语句的规则如下

switch表达式必须是整数或字符类型。

case值必须是整数或字符常量。

case值只能在switch语句中使用。

switch case中的break语句不是必须的。这是一个可选项。如果在switch case中没有使用break语句,则匹配case值后将执行所有后的语句。它被称为通过C语言switch语句的状态。

我们试着通过例子来理解它。假设有以下变量及赋值。

int x,y,z;

char a,b;

float f;

C

C语言switch语句

C语言中的switch语句的流程图

我们来看一个简单的C语言switch语句示例。创建一个源文件: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");

}

}

C

执行上面示例代码,得到以下结果


Enter a number:88

umber is not equal to 10, 50 or 100

Shell

执行第二次,结果如下


Enter a number:50

umber is equal to 50

请按任意键继续. . .Shell

switch语句直通到尾

在C语言中,switch语句是通过的,这意味着如果在switch case中不使用break语句,则匹配某个case之后的所有的case都将被执行。

我们来试试通过下面的例子来了解switch语句的状态。创建一个源文件: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");

}

}

C

执行上面示例代码,得到以下结果


enter a number:10

umber is equals to 10

umber is equal to 50

umber is equal to 100

umber is not equal to 10, 50 or 100

请按任意键继续. . .Shell

从上面的输出结果中,可以清楚地看到,当匹配 number = 10 之后,由于没有break语句,其它后面的语句也打印执行了。

本文内容转载自网络,本着分享与传播的原则,版权归原作者所有,如有侵权请联系我们进行删除。

想要了解更多欢迎拨打电话:4OO-6O6-2328,在线咨询登记享受优惠活动QQ 1159095060 微信AssHaoyun

  • 上一篇:昆明专业c++技术培训学校多少钱
  • 下一篇:昆明有哪些PHP培训班哪个好
  • 相关推荐 更多>

    昆明实力强劲的UI设计培训机构榜...

    昆明大众认可度高的UI设计培训机...

    昆明教学卓越的UI设计培训机构榜...

    昆明在前列的UI设计培训机构...

    昆明几家必看的UI设计培训机构名...

    昆明目前优质的UI设计培训机构重...

    预约体验课

    版权所有:搜学搜课(www.soxsok.com)

  • 在线咨询
  • 电话咨询
  • 预约试听

  • ;