JavaScript Break and Continue
The Break Statement
The break statement can be used to jump out of a loop or a switch() statement.
The break statement breaks the loop and continues executing the code after the loop (if any):
Example
for (i=0;i<10;i++) { if (i==3) { break; } x=x + "The number is " + i + "<br>"; }
The Continue Statement
The continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in the loop.
This example skips the value of 3:
Example
for (i=0;i<=10;i++) { if (i==3) continue; x=x + "The number is " + i + "<br>"; }
JavaScript Labels
As you have already seen, in the chapter about the switch statement, JavaScript statements can be labeled.
To label JavaScript statements you precede the statements with a colon:
label: statementsThe break and the continue statements are the only JavaScript statements that can "jump out of" a code block.
Syntax:
break labelname;continue labelname;
The continue statement (with or without a label reference) can only be used inside a loop.
The break statement, without a label reference, can only be used inside a loop or a switch.
With a label reference, it can be used to "jump out of" any JavaScript code block:
Example
cars=["BMW","Volvo","Saab","Ford"]; list: { document.write(cars[0] + "<br>"); document.write(cars[1] + "<br>"); document.write(cars[2] + "<br>"); break list; document.write(cars[3] + "<br>"); document.write(cars[4] + "<br>"); document.write(cars[5] + "<br>"); }
转载于:https://www.cnblogs.com/Linford-Xu/archive/2013/05/08/3066718.html
- JavaScript Break and Continue
- Break and Continue
- JavaScript Break 和 Continue区别教程
- 浅析JavaScript中break、continue和return的区别
- Javascript基础教程之break和continue语句
- 对比JavaScript中的Continue和Break
- javaScript如何跳出多重循环break、continue
- javascript break 和continue
- javascript中break,continue和return语句用法小结
- JavaScript之基础-7 JavaScript 循环结构 (while、do-while、for及循环中的continue、break)
- JavaScript break和continue 跳出循环
- Java breakand continue
- javascript中关于break,continue的特殊用法与介绍
- JavaScript中Break和Continue
- 跟我学JavaScript--条件语句,循环语句,break语句,continue语句
- JavaScript Break 和 Continue 语句
- 2014年辛星Javascript解读第五节 break continue 与异常处理
- Javascript—Break和Continue语句
- 【Javascript】javascript学习 十四 JavaScript Break 和 Continue
- javaScript如何跳出多重循环break、continue