06-continue和break的区别

区别

(1)continue表示结束本次循环,进入下一次循环。

(2)break表示退出循环,不再执行循环体。

(3)适用场合:break可以在switch语句和循环结构中使用;continue在循环结构中使用。

例子1:

 1 #include <stdio.h>
 2 int main(int argc, const char * argv[])
 3 {
 4     int count = 0;
 5     while(count < 20)
 6     {
 7         ++count;
 8         if(count%5 == 0)
 9         {
10             continue;
11         }
12         printf("做第%d个俯卧撑
",count);
13     }
14     return 0;
15 }

第8-11行代码,continue的作用是:当做俯卧撑的次数为5的倍数的时候,结束本次循环,重新判断条件,执行下一次循环。

输出结果为:

例子2:

 1 #include <stdio.h>
 2 int main(int argc, const char * argv[])
 3 {
 4     int count = 0;
 5     while(count < 20)
 6     {
 7         ++count;
 8         if(count % 5 == 0)
 9         {
10             break;
11         }
12         printf("做第%d个俯卧撑
",count);
13     }
14     return 0;
15 }

第8-11行代码,break的作用:当第一次判断做俯卧撑的次数为5的倍数的时候,就退出循环,不在继续执行下次循环。

输出结果为:

1

人生之路,不忘初心,勿忘始终!
原文地址:https://www.cnblogs.com/xdl745464047/p/4003427.html