600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > C语言 for循环break continue

C语言 for循环break continue

时间:2022-11-18 20:26:46

相关推荐

C语言 for循环break continue

C语言

for循环break、continue

在C语言中我们常会使用break、continue,

这篇博文主要是写,在for循环中break和continue的作用和区别;

continue

#include <stdio.h>int count = 0;int main(int argc, char *argv[]){for (int i = 0; i < 6; i++, count++, printf("count = %d\n", count)) {if (i == 2) {printf("i = %d\n", i);continue;}printf("1111111");}return 0;}

运行结果:

bao:day1015 bao$ gcc -o test2 test2.c

bao:day1015 bao$ ./test2

1111111count = 1

1111111count = 2

i = 2

count = 3

1111111count = 4

1111111count = 5

1111111count = 6

通过运行结果,我们可以得出

1、continue在for循环中的作用是跳出本次循环,进行下一次循环;

2、如果在for调用continue,那么for循环中continue后面的代码不进行作用(printf("1111111");不执行),

但是for循环中的i++, count++, printf("count = %d\n", count)还是会执行.

break

#include <stdio.h>int count = 0;int main(int argc, char *argv[]){for (int i = 0; i < 6; i++, count++, printf("count = %d\n", count)) {if (i == 2) {printf("i = %d\n", i);break;}printf("1111111");}return 0;}

运行结果

bao:day1015 bao$ ./test2

1111111count = 1

1111111count = 2

i = 2

1、break会跳出循环,不会执行break后面的一切代码.

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。