switch-case语句在未加break时的执行顺序

swith case 语句在未加break,并且case语句顺序不按照case后常量表达式的值顺序时,程序执行顺序分析。

程序源文件 switch_case_test.c

#include <stdio.h>

int main(int argc, char *argv[])
{
	int a;
	char fileName[100] = "switch_case_test.c";
	char lineContents[100];
	int i, n;
	FILE *filePointer = NULL;
	filePointer = fopen(fileName, "rt");
	for(i=0; i<100; i++)
	{
		fgets(lineContents, 100, filePointer);
		if((i>21)&&(i<33))
		{
			printf("%s", lineContents);
		}
	}
	fclose(filePointer);

	printf("Pls input integer number:  ");
	scanf("%d", &a);
	switch(a)
	{
		case 1: printf("It is Monday!\n");     
		case 4: printf("It is Thursday!\n");   
		case 5: printf("It is Friday!\n");     
		case 6: printf("It is Saturday!\n");   
		default :printf("Input error number!\n");  
		case 2: printf("It is Tuesday!\n");    
		case 3: printf("It is Wednesday!\n");  
		case 7: printf("It is Sunday!\n");     
	} 
	return 0;
}
程序执行:

maemo@ll:switch_case_test$ ./switch_case_test 
	switch(a)
	{
		case 1: printf("It is Monday!\n");     
		case 4: printf("It is Thursday!\n");   
		case 5: printf("It is Friday!\n");     
		case 6: printf("It is Saturday!\n");   
		default :printf("Input error number!\n");  
		case 2: printf("It is Tuesday!\n");    
		case 3: printf("It is Wednesday!\n");  
		case 7: printf("It is Sunday!\n");     
	} 
Pls input integer number:  5
It is Friday!
It is Saturday!
Input error number!
It is Tuesday!
It is Wednesday!
It is Sunday!
maemo@ll:switch_case_test$ 
以上测试可以看出在case语句后未加入break时,执行完case后的语句将继续执行后续的语句:case 6, default, case 2, case 3, case 7。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值