026.While 循环语句

来源:互联网 发布:关于单片机的毕业设计 编辑:程序博客网 时间:2024/06/09 19:45
---------------  main.m  ---------------
#import<Foundation/Foundation.h>

intmain() // 示例一
{
   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>

intmain() // 示例二 
{
   int count = 0;
   while (count < 10);// 这里的分号导致循环体脱节
    // 分号表示一个空语句,不断执行空语句,其实也是死循环
   // 下面的代码块与while循环已经没有任何关系
    {
        NSLog(@"count: %d", count);
        count++;
    }
}
---------------  main.m ---------------
#import<Foundation/Foundation.h>

intmain() // 示例三
{
   int count = 10;
   while(count)
    {
        NSLog(@"count: %d", count);
        count--;
    }
}

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

二、本节代码涉及到的知识点:
1.循环语句通常包含四个部分:①初始化语句 ②循环条件 ③循环体 ④迭代语句
2.在上面的第三份代码中,while (count),就等于 while (count != 0)。
3.break是指跳出此循环,而continue是指跳出本次循环,进入下一次循环。
0 0
原创粉丝点击