Javascript 在嵌套的 if 语句中调用 break
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4851657/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Call break in nested if statements
提问by Upvote
I have the following situation:
我有以下情况:
IF condition THEN
IF condition THEN
sequence 1
ELSE
break //?
ENDIF
ELSE
sequence 3
ENDIF
What is the result of the break statement? Does it break the outer if statement? Because this is what I actually need.
break 语句的结果是什么?它会破坏外部的 if 语句吗?因为这才是我真正需要的。
采纳答案by hvgotcodes
no it doesnt. break is for loops, not ifs.
不,它没有。break 是 for 循环,不是 ifs。
nested if statements are just terrible. If you can avoid them, avoid them. Can you rewrite your code to be something like
嵌套的 if 语句很糟糕。如果您可以避免它们,请避免它们。你能把你的代码改写成这样吗
if (c1 && c2) {
//sequence 1
} else if (c3 && c2) {
// sequence 3
}
that way you don't need any control logic to 'break out' of the loop.
这样你就不需要任何控制逻辑来“跳出”循环。
回答by Daniel Lewis
If you label the if statement you can use break.
如果您标记 if 语句,则可以使用 break。
breakme: if (condition) {
// Do stuff
if (condition2){
// do stuff
} else {
break breakme;
}
// Do more stuff
}
You can even label and break plain blocks.
您甚至可以标记和破坏普通块。
breakme: {
// Do stuff
if (condition){
// do stuff
} else {
break breakme;
}
// Do more stuff
}
It's not a commonly used pattern though, so might confuse people and possibly won't be optimised by compliers. It might be better to use a function and return, or better arrange the conditions.
不过,这不是常用的模式,因此可能会使人们感到困惑,并且编译器可能不会对其进行优化。最好使用函数并返回,或者更好地安排条件。
( function() {
// Do stuff
if ( condition1 ) {
// Do stuff
} else {
return;
}
// Do other stuff
}() );
回答by Van Coding
In the most languages, break does only cancel loops like for, while etc.
在大多数语言中,break 只会取消 for、while 等循环。
回答by u476945
Javascript will throw an exception if you attempt to use a break;
statement inside an if else. It is used mainly for loops. You can "break" out of an if else statement with a condition, which does not make sense to include a "break" statement.
如果您尝试break;
在 if else 中使用语句,Javascript 将抛出异常。它主要用于循环。您可以“中断”带有条件的 if else 语句,这对于包含“break”语句是没有意义的。
回答by Robert
Actually there is no c3 in the sample code in the original question. So the if would be more properly
实际上原始问题的示例代码中没有 c3 。所以如果会更合适
if (c1 && c2) {
//sequence 1
} else if (!c1 && !c2) {
// sequence 3
}
回答by Crazometer
I had a similar problem today and found refactoring the conditional logic into a separate function to help.
我今天遇到了类似的问题,发现将条件逻辑重构为一个单独的函数来提供帮助。
I find it more readable than the labels and people are more comfortable with return
than break
. Inline functions are similar but the indentation can get a bit confusing.
我发现它比标签更具可读性,而且人们return
比break
. 内联函数是相似的,但缩进可能会有点混乱。
In your case it would look like this.
在你的情况下,它看起来像这样。
function doThing() {
checkConditions();
// Rest of the code here
}
function checkConditions() {
if (c1) {
if (c2) {
return do1();
else {
return;
}
} else {
return do3();
}
}
回答by Doctor Rudolf
To make multiple checking statements more readable (and avoid nested ifs):
为了使多个检查语句更具可读性(并避免嵌套 ifs):
var tmp = 'Test[[email protected]]';
var posStartEmail = undefined;
var posEndEmail = undefined;
var email = undefined;
do {
if (tmp.toLowerCase().substring(0,4) !== 'test') { break; }
posStartEmail = tmp.toLowerCase().substring(4).indexOf('[');
posEndEmail = tmp.toLowerCase().substring(4).indexOf(']');
if (posStartEmail === -1 || posEndEmail === -1) { break; }
email = tmp.substring(posStartEmail+1+4,posEndEmail);
if (email.indexOf('@') === -1) { break; }
// all checks are done - do what you intend to do
alert ('All checks are ok')
break; // the most important break of them all
} while(true);
回答by D Nootana
Just remove the break. since it is already inside first if it will not execute else. It will exit anyway.
只需删除中断。因为如果它不会执行其他,它已经在里面了。无论如何它都会退出。
回答by miqbal
You need that it breaks the outer if statement. Why do you use second else?
您需要它打破外部 if 语句。你为什么使用第二个其他?
IF condition THEN
IF condition THEN
sequence 1
// ELSE sequence 4
// break //?
// ENDIF
ELSE
sequence 3
ENDIF
sequence 4