# if else if 和嵌套 if 之间有相似之处吗？

go评论45阅读模式

Is there any similarity between if else if and nested if?

# 问题

nested if

if(条件)
{
if(条件)
{

}
else
{

}
}
else
{
if(条件)

else

}

if else if

if(条件)

else if(条件)

else

``````  nested if

**if(condition)**
{
**if(condition)**
{
statement-1;
}
else
{
statement-2;
}
}
else
{
if(condition)
statement-3;
else
statement-4;
}

if else if

if(condition)
statement-1;
**else if**(condition)
statement-2;
else
statement-3;
``````

ANSWER:Look above syntax of both bidirectional. The nested if is an if statement used within another if statement.When we use if else if then an if statement is used within the else part of another if in this way,'nested if is similar to an if else if statement.

i did not understand above answer.

# 答案1

"如果"我理解你的意思（这个双关语实际上并不是故意的）...

``````if (someCondition) {
//...
} else if (anotherCondition) {
//...
}
``````

``````if (someCondition) {
//...
} else {
if (anotherCondition) {
//...
}
}
``````

``````if (someCondition)
//... 一个语句
else
if (anotherCondition) {
//... 更多语句
}
``````

"If" I'm understanding you correctly (pun not really intended)...

You can often think of `else if` as taking advantage of the fact that, in the absence of curly braces, an `if` or `else` "block" will be the next statement. And if that next statement is an `if` then the whole structure of that `if` is considered to be that statement.

What that means is this:

``````if (someCondition) {
//...
} else if (anotherCondition) {
//...
}
``````

Is the same as this:

``````if (someCondition) {
//...
} else {
if (anotherCondition) {
//...
}
}
``````

It becomes a little more clear if you remove more of the curly braces, and move the second `if` to its own line. This is also the same as the above:

``````if (someCondition)
//... one statement
else
if (anotherCondition) {
//... more statements
}
``````

Structurally these are all the same. And the compiler may even produce the same resulting output. (Though I wouldn't be surprised if it added more tweaks for efficiency and such. Compilers are good at that sort of thing.)

So basically an `else if` is just an `else` with no curly braces, where the only statement within that `else` block is another `if` structure.

# 答案2

``````if (a) {
if (b) {
//  a && b
} else {
//  a && !b
}
} else {
//  !a
}
``````
``````if (a) {
// a
} else if (b) {
// !a && b
} else {
// !a && !b
}
``````

They don't cover the same use cases. For exemple in the nested if there is -1 when both conditions a and b are true, but in the else if it only requires the condition a.

Here is the differences

``````    if (a) {
if (b) {
//  a &amp;&amp; b
} else {
//  a &amp;&amp; !b
}
} else {
//  !a
}
``````
``````    if (a) {
// a
} else if (b) {
// !a &amp;&amp; b
} else {
// !a &amp;&amp; !b
}
``````

# 答案3

``````if (condition-a) {
if (condition-b)
statement-1;
else
statement-2;
} else
statement-3;
``````

false false => statement-3 | 非A
false true => statement-3 |
true false => statement-2 | A 且 非B
true true => statement-1 | A 且 B

``````if (condition-a)
statement-1;
else if (condition-b)
statement-2;
else
statement-3;
``````

false false => statement-3 | 非A 且 非B
false true => statement-2 | 非A 且 B
true false => statement-1 | A
true true => statement-1 |

The they are similar. But they are not the same.

One way to understand the differences is to write out the "truth tables" for the two versions. Note that we need to distinguish the two conditions to understand the nature of the differences.

``````# version 1
if (condition-a) {
if (condition-b)
statement-1;
else
statement-2;
} else
statement-3;
``````

``````condition-a   condition-b  =&gt;
false         false     =&gt; statement-3   | NOT A
false         true      =&gt; statement-3   |
true          false     =&gt; statement-2   | A AND NOT B
true          true      =&gt; statement-1   | A AND B
``````

Now the alternative version

``````# version 2
if (condition-a)
statement-1;
else if (condition-b)
statement-2;
else
statement-3;
``````

``````condition-a   condition-b  =&gt;
false         false     =&gt; statement-3   | NOT A AND NOT B
false         true      =&gt; statement-2   | NOT A AND B
true          false     =&gt; statement-1   | A
true          true      =&gt; statement-1   |
``````

As you can see, the truth tables are different. I have also added the equivalent "boolean logic"

OK, so how did I work out the truth tables? Answer: by "hand executing" the code. You can do the same thing to check that I have gotten the tables correct AND to understand the logic behind the differences.

# 答案4

“else”和“if”是两个单独的关键词，因此您可以分开并进行缩进以提高可读性。

``````if (condition)
statement-1;
else
if (condition)
statement-2;
else
statement-3;
``````

If you are trying to understand the answer that you have posted, yes, the last `if` condition in a "if else if" condition can be called as a "nested if condition".

`else` and `if` are two separate keywords, so you can split and indent for better readability.

``````  if(condition)
statement-1;
else
if(condition)
statement-2;
else
statement-3;
``````

• 本文由 发表于 2020年7月30日 00:04:50
• 转载请务必保留本文链接：https://go.coder-hub.com/63157782.html
• java

go 68

go 36

go 82

go 47