关于c语言的switch用法

c语言的switch用法

分析思路如下:

a=1

case 1 成立

如果这个case 有break的话,那面后面所有的case都不用看了。

case 2 成立

case3 同样道理

default :中文意思是:默认的、缺省的意思。

所以条件都不符合的时候要执行的。

示例代码:

#include "stdio.h"

#include "conio.h"

main()

{

int a=1;

switch(a)

{

case 1:

printf("case 1\n");

case 2:

printf("case 2\n");

case 3:

printf("case 3\n");

default: printf("defaule!");

}

getch();

}

结果:

case 1

case 2

case 3

defaule!

示例2 有break

#include "stdio.h"

#include "conio.h"

main()

{

int a=1;

switch(a)

{

case 1:

printf("case 1\n");

case 2:

printf("case 2\n");

break;

case 3:

printf("case 3\n");

default: printf("defaule!");

}

getch();

}

case 1

case 2

示例3 一个条件都不符合

#include "stdio.h"

#include "conio.h"

main()

{

int a=1;

switch(100)

{

case 1:

printf("case 1\n");

case 2:

printf("case 2\n");

break;

case 3:

printf("case 3\n");

default: printf("defaule!");

}

getch();

}

结果:

default!

原文地址:https://www.weidianyuedu.com/content/2220674832754.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值