[英]Does JavaScript check remaining conditions in “if”–“else if”–“else” after the first condition is true?
In JavaScript, if you have an if
… else if
… else
situation, does the computer keep checking the next condition if it previously triggered another condition? 在JavaScript中,如果遇到if
… else if
… else
情况,如果计算机先前触发了另一个条件,计算机是否继续检查下一个条件? Eg if the first if
condition is true and the code executes, does it still attempt to check the conditions for else if
or else
? 例如,如果第一if
条件为真,代码执行,它仍试图检查的条件else if
还是else
?
Say you have a code segment 假设您有一个代码段
if(a)
func1()
else if(b)
func2()
else if(c)
func3()
else
func4();
//next statement
If a
is true func1()
will be called and, after it returns, the control will go to next statement. 如果a
为true,则将调用func1()
并在返回之后,控件将转到下a
语句。
Accordingly if b
is true then the at first func2()
will be called and then, after it returns, the control will go to next statement. 因此,如果b
为true,则将首先func2()
,然后在返回后,控件将转到下一条语句。
func4()
will be called if a
, b
, c
all of them are false. 如果a
, b
, c
均为假,则会调用func4()
。 After func4()
returns control goes to next statement. func4()
返回后,控制权转到下func4()
语句。
No it won't check if the next condition is true, since you are using else if
. 不,它不会检查下一个条件是否成立,因为您正在使用else if
。
Instead, if you use if
… if
… if
, then all if
statement will be executed. 相反,如果使用if
… if
… if
,则将执行所有if
语句。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.