`
123fly
  • 浏览: 11275 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

Break 和 Continue 语句

阅读更多

break 语句用于跳出循环。
continue 用于跳过循环中的一个迭代。
下面分别介绍一下break语句和continue的用法:
Break
break 语句可用于跳出循环。
break 语句跳出循环后,会继续执行该循环之后的代码。
js代码

for (i=0;i<10;i++)
  {
    var x;
  if (i==3)
     break;
    console.log( x="The number is" + i);
  }

 
代码输出结果是:
The number is 0
The number is 1
The number is 2
Continue
continue 语句中断循环中的迭代,如果出现了指定的条件,然后继续循环中的下一个迭代。
js代码

for (i=0;i<=10;i++)
{ 
  var x;
if (i==3) continue;
console.log( x="The number is "+i );
  }

 
代码输出结果
The number is 0
The number is 1
The number is 2
The number is 4
The number is 5
The number is 6
The number is 7
The number is 8
The number is 9
通过这两个例子的对比更好的说明了Break 和 Continue 语句的不同用法。

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics