您的位置:首页 > 其它

026.While 循环语句

2015-10-27 15:35 183 查看
---------------  main.m  ---------------

#import <Foundation/Foundation.h>

int main() //
示例一

{

    int count = 0;

    while (count < 10)

    {

        NSLog(@"count:%d", count);

        count++;

    }

    NSLog(@"循环结束!");

    // 下面是一个死循环

    int count2 = 0;

    while (count2 < 10)

    {

        NSLog(@"不停执行的死循环 %d",
count2);

        count2--;

    }

    NSLog(@"永远无法跳出的循环体");

}

---------------  main.m  ---------------

#import <Foundation/Foundation.h>

int main() //
示例二 

{

    int count = 0;

    while (count < 10); //
这里的分号导致循环体脱节

    // 分号表示一个空语句,不断执行空语句,其实也是死循环

    // 下面的代码块与while循环已经没有任何关系

    {

        NSLog(@"count: %d", count);

        count++;

    }

}

---------------  main.m  ---------------
#import <Foundation/Foundation.h>

int main() //
示例三

{

    int count = 10;

    while (count)

    {

        NSLog(@"count: %d", count);

        count--;

    }

}

一、编写本节代码的具体步骤:
1.参照003节的代码编写步骤。

二、本节代码涉及到的知识点:

1.循环语句通常包含四个部分:①初始化语句 ②循环条件 ③循环体 ④迭代语句

2.在上面的第三份代码中,while (count),就等于 while (count != 0)。

3.break是指跳出此循环,而continue是指跳出本次循环,进入下一次循环。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: