概述
Nesting if/else statements helps to organize and isolate conditions in order to avoid testing the same condition twice or to minimize the number of times various tests need to be performed.
嵌套if / else语句有助于组织和隔离条件,从而避免对同一条件进行两次测试或将需要执行各种测试的次数降至最低。
By using if statements with both comparison and logical operators, we can set up code that will be run if a specific combination of conditions is met. We don't always want to test the entire condition in order to run one set of statements if the entire test is true, and another if it is false. We may want to choose between several different statements, depending on which particular combination of conditions is true.
通过同时使用具有比较运算符和逻辑运算符的if语句,我们可以设置在满足特定条件组合时将运行的代码。 如果整个测试为真,我们并不总是要测试整个条件以便运行一组语句,如果测试为假,则运行另一组语句。 我们可能要在几种不同的陈述中选择,这取决于条件的特定组合是否正确。
Suppose, for example, that we have three values to compare and wish to set different results depending on which of the values are equal. The following example shows how we can nest if statements to test for this (in bold below)
例如,假设我们有三个要比较的值,并希望根据哪个值相等来设置不同的结果。 以下示例说明了如何嵌套if语句以对此进行测试(下面以粗体显示)
var answer;
if (a == b) {
if (a == c) {
answer = "all are equal";
答案=“人人平等”;
} else {
}其他{
answer = "a and b are equal";
答案=“ a和b相等”;
}
}
} else {
if (a == c) {
answer = "a and c are equal";
} else {
if (b == c) {
answer = "b and c are equal";
答案=“ b和c相等”;
} else {
}其他{
answer = "all are different";
答案=“一切都不同”;
}
}
}
}
The way the logic works here is:
逻辑在这里的工作方式是:
If the first condition is true (
如果第一个条件为真(
nested if condition (嵌套的if条件(if (a == b)
), then the program checks for the
else condition.else条件。if (a == c)
). If the first condition is false, the program bumps to theIf the nested if is true, the statement is executed, i.e. "all are equal".
如果嵌套的if为true,则执行该语句,即“全部相等”。
If the nested if is false, then the else statement is executed, i.e. "a and b are equal".
如果嵌套的if为假,则执行else语句,即“ a和b相等”。
Here are a few things to notice how this is coded:
以下是一些注意事项,以了解其编码方式:
First, we created the variable answer to hold the result before we started the if statement, making the variable global. Without that, we would have needed to include the variable on the front of all of the assignment statements, since it would be a local variable.
首先,在启动if语句之前,我们创建了变量answer来保存结果,从而使变量成为global 。 否则,我们将需要在所有赋值语句的前面都包含变量,因为它将是局部变量。
Secondly, we have indented each nested if statement. This allows us to track more easily how many nested levels of statements there are. It also makes it clearer that we have closed the right number of blocks of code to complete all of the if statements that we opened. You may find that it is easier to put the braces there first for each if statement before you start writing the code that belongs inside that block.
其次,我们缩进了每个嵌套的if语句。 这使我们可以更轻松地跟踪有多少嵌套的语句级别。 这也使我们更清楚地了解到,我们已经关闭了正确数量的代码块,以完成我们打开的所有if语句。 您可能会发现,在开始编写属于该块内部的代码之前,将每个if语句的花括号放在其中比较容易。
We can simplify one section of this code slightly in order to avoid having to nest the if statements quite as much. Where an entire else block is made up of a single if statement, we can omit the braces around that block and move the if condition itself up onto the same line as the else, using the "else if" condition. For example:
我们可以略微简化这段代码的一部分,以免不得不过多地嵌套if语句。 在整个else块由单个if语句组成的情况下,我们可以忽略该块周围的花括号,并使用“ else if”条件将if条件本身移至与else相同的行。 例如:
var answer;
if (a == b) {
if (a == c) {
answer = "all are equal";
} else {
answer = "a and b are equal";
}
} else if (a == c) {
answer = "a and c are equal";
答案=“ a和c相等”;
} else if (b == c) {
}否则,如果(b == c){
answer = "b and c are equal";
答案=“ b和c相等”;
} else {
answer = "all are different";
}
Nested if/then statements are common in all programming languages, not just JavaScript. Novice programmers often use multiple if/then or if/else statements rather than nesting them. While this kind of code will work, it will quickly become verbose and will duplicate conditions. Nesting conditional statements creates more clarity around the program's logic and results in concise code that may run or compile faster.
嵌套的if / then语句在所有编程语言中都是常见的,而不仅仅是JavaScript 。 新手程序员经常使用多个if / then或if / else语句,而不是嵌套它们。 虽然这种代码可以工作,但很快就会变得冗长,并会重复条件。 嵌套条件语句可以使程序逻辑更加清晰,并生成简洁的代码,从而可以更快地运行或编译 。
翻译自: https://www.thoughtco.com/javascript-making-decisions-2037427
最后
以上就是怕孤独铃铛为你收集整理的JavaScript嵌套的IF / ELSE语句的全部内容,希望文章能够帮你解决JavaScript嵌套的IF / ELSE语句所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复