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