找回密码
 注册
搜索

[编程] C语言continue和break有的区别

已有 118 次阅读2013-8-22 11:09 |个人分类:编程技巧| continue, C语言

continue语句用来返回循环的起始处,而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 * /
     }
    这个例子建立在使用continue语句的那个例子的基础上。注意,在这个例子中,要检查parse_line()函数的返回值。如果parse_line()的返回值为FATAL_ERROR,就通过break语句立即退出while循环,并将控制权交给循环后面的第一条语句。

路过

雷人

握手

鲜花

鸡蛋

评论 (0 个评论)

facelist

您需要登录后才可以评论 登录 | 注册

Archiver|手机版|小黑屋|52RD我爱研发网 ( 沪ICP备2022007804号-2 )

GMT+8, 2024-9-29 19:29 , Processed in 0.028945 second(s), 17 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

返回顶部