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

huangapple go评论73阅读模式
英文:

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

问题

nested if

if(条件)
{
if(条件)
{
语句-1;
}
else
{
语句-2;
}
}
else
{
if(条件)
语句-3;
else
语句-4;
}

if else if

if(条件)
语句-1;
else if(条件)
语句-2;
else
语句-3;
回答: 请查看上述两者的语法。嵌套if是在另一个if语句内部使用的if语句。当我们使用if else if时,if语句是在另一个if语句的else部分中使用的,这样说,'嵌套if与if else if语句类似'。如果您仍然不明白,请告诉我需要进一步解释什么。

英文:
  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

得分: 7

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

你可以常常把 else if 想象成利用了一个事实,即在缺少花括号的情况下,一个 ifelse "块" 将成为下一个语句。如果下一个语句是一个 if,那么整个结构都被视为该语句。

这意味着:

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

与此相同:

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

如果你移除更多花括号并将第二个 if 移动到自己的一行,这将变得更加清晰。这也与上面的代码相同:

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

从结构上看,它们都是相同的。而且编译器甚至可能会产生相同的输出结果。(尽管我不会感到惊讶,如果它为了效率等方面添加了更多的优化。编译器在这方面做得很好。)

所以基本上,else if 只是一个没有花括号的 else,在该 else 块中唯一的语句是另一个 if 结构。

英文:

"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

得分: 1

它们不涵盖相同的用例。例如,在嵌套的if语句中,当条件a和b都为真时,会出现-1,但在else if语句中,只需要条件a。

以下是它们的区别:

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 && b
      } else {
        //  a && !b
      }
    } else { 
      //  !a
    }
    if (a) {
      // a
    } else if (b) {
      // !a && b
    } else {
      // !a && !b
    } 

答案3

得分: 0

它们相似,但它们不同。

要理解它们之间的区别,一种方法是为这两个版本编写“真值表”。请注意,我们需要区分这两个条件以理解区别的性质。

版本1

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

条件-a 条件-b =>
false false => statement-3 | 非A
false true => statement-3 |
true false => statement-2 | A 且 非B
true true => statement-1 | A 且 B

现在是另一种版本

版本2

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

条件-a 条件-b =>
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  =>
   false         false     => statement-3   | NOT A
   false         true      => statement-3   |
   true          false     => statement-2   | A AND NOT B
   true          true      => 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  =>
   false         false     => statement-3   | NOT A AND NOT B
   false         true      => statement-2   | NOT A AND B
   true          false     => statement-1   | A
   true          true      => 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

得分: 0

如果您试图理解您发布的答案,是的,在“if else if”条件中的最后一个“if”条件可以称为“嵌套if条件”。

“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;

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

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定