Objective-C 中的 continue 语句



在 Objective-C 编程语言中,continue 语句的工作方式与break 语句有些类似。但是,它不会强制终止循环,而是强制执行循环的下一轮迭代,跳过中间的任何代码。

对于for 循环,continue 语句会导致循环的条件测试和增量部分执行。对于whiledo...while 循环,continue 语句会导致程序控制跳转到条件测试。

语法

Objective-C 中continue 语句的语法如下:

continue;

流程图

Objective-C continue statement

示例

#import <Foundation/Foundation.h>
 
int main () {
   
   /* local variable definition */
   int a = 10;

   /* do loop execution */
   do {
      if( a == 15) {
         /* skip the iteration */
         a = a + 1;
         continue;
      }
      NSLog(@"value of a: %d\n", a);
      a++;
     
   } while( a < 20 );
   return 0;
}

编译并执行上述代码后,将产生以下结果:

2013-09-07 22:20:35.647 demo[29998] value of a: 10
2013-09-07 22:20:35.647 demo[29998] value of a: 11
2013-09-07 22:20:35.647 demo[29998] value of a: 12
2013-09-07 22:20:35.647 demo[29998] value of a: 13
2013-09-07 22:20:35.647 demo[29998] value of a: 14
2013-09-07 22:20:35.647 demo[29998] value of a: 16
2013-09-07 22:20:35.647 demo[29998] value of a: 17
2013-09-07 22:20:35.647 demo[29998] value of a: 18
2013-09-07 22:20:35.647 demo[29998] value of a: 19
objective_c_loops.htm
广告
© . All rights reserved.