您的位置:首页 > 编程语言 > C语言/C++

【c/c++】break和continue

2016-04-21 10:12 387 查看
#include <stdio.h>
#include <conio.h>  //getch()

/*break已经在switch-case中发挥很大的作用,还可以用于for、while、do while中,用于跳出循环,执行循环后面的语句*/
int main(void){
int i = 0;
char c;
while (1){  /*设置循环*/
c = '\0';  /*变量赋初值*/
while (c != 13 && c != 27){  /*键盘接收字符直到按回车或Esc键*/
c = getch();
printf("%c\n", c);
}
if (c == 27){
break;          /*判断若按Esc键则退出循环*/
}
i++;
printf("The No. is %d\n", i);
}
printf("The end");
return 0;
}

/*continue是加速循环,跳过这一次循环,进入下一次循环,而不是说去执行循环后面的语句*/
//int main(void){
//	char c='\0';
//	while (c!= 13){      /*不是回车符则循环*/
//		c = getch();
//		if (c == 53)	//字符53代表数字5
//			continue; /*若数字5不输出便进行下次循环*/
//		printf("%c\n", c);
//	}
//	return 0;
//}

//int main(void){
//	int i = 0;
//	char c;
//	while (1){  /*设置循环*/
//		c = '\0';  /*变量赋初值*/
//		c = getch();
//		if (c == 53){
//			printf("%c\n", c);
//		}
//		else{
//					//break语句对if-else的条件语句不起作用;
//			break;  //在if-else种,break的功效已经体现了啊。
//		}
//	}
//	printf("The end");
//	return 0;
//}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: