JavaScript中的break和continue语句介绍

Break 语句

break 语句用于跳出 switch() 语句。

break 语句可用于跳出循环。

break 语句跳出循环后,会继续执行该循环之后的代码(如果有的话)

for (i=0;i<10;i++)
  {
  if (i==3)
    {
    break;
    }
  x=x + "The number is " + i + "<br>";
  }

运行结果:

The number is 0
The number is 1
The number is 2

 

Continue 语句

continue 语句中断循环中的迭代,如果出现了指定的条件,然后继续循环中的下一个迭代。

该例子跳过了值 3:

for (i=0;i<10;i++)
{
if (i==3) continue;
  x=x + "The number is " + i + "<br>";
  }

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

 

posted on 2014-05-20 12:47  新西兰程序员  阅读(180)  评论(0)    收藏  举报