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 <stdio.h>
#include <stdlib.h>

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是跳过 循环体中的语句,将控制权交给离他最近的循环开始处,所以对条件进行改变的语句还是要执行的。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值