C语言continue和break有的区别

continue与break用法示例
本文通过两个示例介绍了编程中continue与break语句的使用方法。continue用于跳过循环中的当前迭代并开始下一次迭代,而break则用于完全退出循环。文中还涉及了文件读取与解析过程。
ontinue语句用来返回循环的起始处,而break语句用来退出循环。例如,下例中就有一条典型的continue语句:
    while(!feof(infile))
    {
       fread(inbuffer,80,1,infile);/*read in a line from input file*/
       if(!strncmpi(inbuffer,"REM",3))  /*check if it is
                                             a comment line*/
       continue;    /*it's a comment,so jump back to the while()*/
       else
      parse_line();    /*not a comment—parse this line*/
    }
上例读入一个文件并对其进行分析。“REM(remark的缩写)”用来标识正在被处理的文件中的一个注释行。因为注释行对程序不起任何作用,所以可以跳过它。在读入输入文件的每一行时,上例就把该行的前三个字母与"REM"进行比较。如果匹配,则该行就是注释行,于是就用continue语句返回到while语句,继续读入输入文件的下一行;否则,该行就是一条有效语句,于是就调用parse_line()函数对其进行分析。

break语句用来退出循环。下面是一个使用break语句的例子:
while (! feof(infile))
     fread(inbuffer,80,1,infile) ;/* read in a line from input file * /
     if (! strncmpi (inbuffer,"REM",3))  / * check if it is
                                                           a comment line * /
           continue;       /* it's a comment, so jump back to the while() * /
     else
     {
           if (parse_line()==FATAL_ERROR)   / * attempt to parse
                                                                    this line * /
              break;                 /* fatal error occurred,so exit the loop * /

     }

http://v.17173.com/playlist_16243005.html
http://v.17173.com/playlist_16244048.html
http://v.17173.com/playlist_16244589.html
http://v.17173.com/playlist_16245740.html
http://v.17173.com/playlist_16246511.html
http://v.17173.com/playlist_16276445.html
http://v.17173.com/playlist_16275379.html
http://v.17173.com/playlist_16274196.html
http://v.17173.com/playlist_16272743.html
http://v.17173.com/playlist_16271745.html
http://v.17173.com/playlist_16270694.html
http://v.17173.com/playlist_16269747.html
http://v.17173.com/playlist_16268962.html
http://v.17173.com/playlist_16267988.html
http://v.17173.com/playlist_16266881.html
http://v.17173.com/playlist_16265728.html
http://v.17173.com/playlist_16264745.html
http://v.17173.com/playlist_16263755.html
http://v.17173.com/playlist_16262692.html
http://v.17173.com/playlist_16260273.html
http://v.17173.com/playlist_16250327.html
http://v.17173.com/playlist_16249446.html
http://v.17173.com/playlist_16248725.html
http://v.17173.com/playlist_16248141.html
http://v.17173.com/playlist_16247142.html
http://v.17173.com/playlist_16301211.html
http://v.17173.com/playlist_16299899.html
http://v.17173.com/playlist_16298388.html
http://v.17173.com/playlist_16297123.html
http://v.17173.com/playlist_16296085.html
http://v.17173.com/playlist_16294578.html
http://v.17173.com/playlist_16293492.html
http://v.17173.com/playlist_16292032.html
http://v.17173.com/playlist_16290619.html
http://v.17173.com/playlist_16289611.html
http://v.17173.com/playlist_16288604.html
http://v.17173.com/playlist_16286938.html
http://v.17173.com/playlist_16285846.html
http://v.17173.com/playlist_16284744.html
http://v.17173.com/playlist_16283600.html
http://v.17173.com/playlist_16282325.html
http://v.17173.com/playlist_16281018.html
http://v.17173.com/playlist_16279921.html
http://v.17173.com/playlist_16279035.html
http://v.17173.com/playlist_16277617.html
http://v.17173.com/playlist_16324284.html
http://v.17173.com/playlist_16323278.html
http://v.17173.com/playlist_16322226.html
http://v.17173.com/playlist_16321011.html
http://v.17173.com/playlist_16320132.html
http://v.17173.com/playlist_16319146.html
http://v.17173.com/playlist_16318138.html
http://v.17173.com/playlist_16317154.html
http://v.17173.com/playlist_16316170.html
http://v.17173.com/playlist_16315099.html
http://v.17173.com/playlist_16313923.html
http://v.17173.com/playlist_16313062.html
http://v.17173.com/playlist_16311883.html
http://v.17173.com/playlist_16310039.html
http://v.17173.com/playlist_16308938.html
http://v.17173.com/playlist_16307556.html
http://v.17173.com/playlist_16306309.html
http://v.17173.com/playlist_16305034.html
http://v.17173.com/playlist_16303450.html
http://v.17173.com/playlist_16302296.html


在C语言中,continuebreak是两个重要的控制流语句,它们在循环`switch`语句中有不同的用法作用。 ### break语句的用法 - **在循环中**:`break`语句用于跳出循环(`for`、`while`、`do - while`循环)。当`break`语句在循环内部被执行时,循环会立即终止,程序控制流会跳转到循环之后的下一条语句。例如,在嵌套循环中,当`j`等于2时,内层循环会被终止,但外层循环会继续。若在内层循环之后检查了`j`的值,并使用了另一个`break`语句,外层循环也会被终止;若去掉这个额外的`break`语句,外层循环会继续执行,但内层循环将不再执行(因为`j`的值已经超过了内层循环的范围) [^3][^4]。 - **在`switch`语句中**:`break`用于终止`switch`块的执行,防止代码“贯穿”到下一个`case`。例如,在以下代码中: ```c #include<stdio.h> int main(){ int x; scanf("%d",&x); switch(x){ case 1:printf("红");break; case 0:printf("白");break; } } ``` 当`x`为1时,输出“红”,遇到`break`后,`switch`语句结束;当`x`为0时,输出“白”,遇到`break`后,`switch`语句结束 [^2][^3]。 ### continue语句的用法 `continue`语句用于跳过当前循环的剩余部分,并开始下一次循环迭代。在`for`循环中,执行`continue`后,会先执行更新表达式,再进行条件判断;在`while``do - while`循环中,执行`continue`后,会直接进行条件判断。 ### 两者的区别 - **作用范围**:`break`语句可以用于循环语句`switch`语句;而`continue`语句只能用于循环语句。 - **执行效果**:`break`语句会完全终止当前所在的循环或`switch`语句;`continue`语句只是跳过当前循环的剩余部分,然后继续下一次循环迭代。 ### 示例代码 ```c #include <stdio.h> int main() { // break示例 for (int i = 0; i < 5; i++) { if (i == 2) { break; } printf("break示例: %d\n", i); } // continue示例 for (int j = 0; j < 5; j++) { if (j == 2) { continue; } printf("continue示例: %d\n", j); } return 0; } ``` 在上述代码中,`break`示例里,当`i`等于2时,循环终止;`continue`示例里,当`j`等于2时,跳过本次循环的`printf`语句,继续下一次循环。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值