1、while循环语句打印数字1-10:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> //while循环语句打印数字1-10 int main() { int i = 1; while (i <= 10) { printf("%dn", i); i++; } return 0; }
输出结果如下:
2、while循环语句中的break用于永久终止循环:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> int main() { int i = 1; while (i <= 10) { printf("%dn", i); i++; if (i == 5) { break;//永久终止循环 } } return 0; }
输出结果如下:
3、
while循环语句中的continue用于终止本次循环,它后边的代码不再执行,直接跳转到while语句的判断部分,进行下一次循环的入口判断。
例如:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> int main() { int i = 0; while (i <= 10) { i++; if (i == 5) { continue; //当i=5时,continue终止本次循环,不再执行下面的语句, //跳到while循环判断,进行下一次循环的入口判断。 } printf("%dn", i); } return 0; }
执行结果如下图所示:
最后
以上就是优秀鞋垫最近收集整理的关于while循环语句中break和continue的区别和用法的全部内容,更多相关while循环语句中break和continue内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复