continue 遇到各种循环

以前一直以为contiue就是跳过continue后面的语句而直接到循环的开始处,但是今天在Linux 0.11 的fs/buffer.c发现

struct buffer_head *getblk(...)
{
    ....
    tmp = free_list;
    do{
            if(tmp->b_count)
                continue;
            ....
         }while((tmp = tmp->b_next_free) !=free_list);
       ....
     }
读到这段代码时在这个地方想了好久,以为发现了bug,查了下资料没有出错。自己就写了个程序验证了下,果然,是自己基础太差了。

首先,看一下do-while的:

#include 
#include 

int main(void)
{
     int i = 0;
     do{
             if(i == 0)
                continue;
             printf("%d\n",i);
         }while((i++) < 5);
      return 0;
}
这个确实不是死循环,while中的i++执行了。

for循环和while循环比较好理解。

看一下msdn中的解释:

The continue statement passes control to the next iteration of the 
nearest enclosing do, for, or while statement in which it appears, 
bypassing any remaining statements in the do, for, or while statement body.
continue是跳过 循环体中的语句,将控制权交给离他最近的循环开始处,所以对条件进行改变的语句还是要执行的。


你可能感兴趣的:(continue 遇到各种循环)