admin管理员组文章数量:1424881
My confusion stems from this example labelled statement:
myLoop : while (expression) {
continue myLoop;
}
and the syntax of a general labelled statement:
identifier : statement
What exactly is being labelled in the example?
isn't the whole block of code:
while (expression)
statement
considered a single statement? Or is while(expression)
itself a statement? Or is while
a statement by itself?
Why isn't the entire:
while (expression) {
continue myLoop;
}
labelled under myLoop
and not just while(expression)
. Or is that even happening?
My confusion stems from this example labelled statement:
myLoop : while (expression) {
continue myLoop;
}
and the syntax of a general labelled statement:
identifier : statement
What exactly is being labelled in the example?
isn't the whole block of code:
while (expression)
statement
considered a single statement? Or is while(expression)
itself a statement? Or is while
a statement by itself?
Why isn't the entire:
while (expression) {
continue myLoop;
}
labelled under myLoop
and not just while(expression)
. Or is that even happening?
2 Answers
Reset to default 6I'd never seen labelled while loops before, but according to this http://james.padolsey./javascript/looping-in-javascript/ it is the whole while loop that is being labelled.
The use for it is to break out of a particular loop, handy with loops-in-loops e.g. (example taken from the link)
myOuterLoop : while (condition) {
myInnerLoop : while (condition) {
if (whatever) {
break myOuterLoop;
}
if (whatever2) {
break; // Same as 'break myInnerLoop;'
}
}
}
What's being labelled isn't a block of code, it's just a particular line. So wherever your label myLoop is, writing continue myLoop is like saying "jump to that spot and continue execution".
But actually, in this example:
myLoop : while (expression) {
continue myLoop;
}
The use of the label is pletely redundant. You would write it as follows and the effect would be identical:
while (expression) {
continue;
}
That's because continue by default means, go to the beginning of the loop's next iteration.
本文标签: statementsLabeled while loops in JavascriptStack Overflow
版权声明:本文标题:statements - Labeled while loops in Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745422010a2657942.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论