在 Java 中,这是否被视为“嵌套 IF 语句”的示例?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3639220/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 03:02:06  来源:igfitidea点击:

In Java, is this considered an example of a "nested IF statement"?

javanestedif-statement

提问by swajak

Here we have a long-standing assumption that needs to be cleared up in my head. Is the following an example of nesting 'if' statements:

在这里,我们有一个长期存在的假设需要在我的脑海中澄清。以下是嵌套“if”语句的示例:

if (...)
  ...;
else if (...)
  ...;

I was under the impression that nesting required an 'if' inside another 'if', like so:

我的印象是嵌套需要在另一个“if”中包含一个“if”,如下所示:

if (...)
  if (...)
    ...;

or at least a clear separation of scope when you nest inside an else, like so:

或者至少在嵌套在 else 中时明确分离范围,如下所示:

if (...)
  ...;
else { //if the next statement didn't 
       //exist, then the curly brace changes nothing?
  ...;
  if (...)
    ...;
}

This might boil down to how the compiler interprets things, whether the 'if' in else-ifs are considered in the same level as the parent if, or whether they create "new" 'if' statements. Thank you for your time!

这可能归结为编译器如何解释事物,else-ifs 中的“if”是否被认为与父 if 处于同一级别,或者它们是否创建“新”“if”语句。感谢您的时间!

edit: I ask because I am a TA in a java lab, and the topic of the day was nested-ifs. In the end, I found out that the teacher considered my first example to be valid for "nested if statements".

编辑:我问是因为我是 Java 实验室的助教,当天的主题是嵌套 ifs。最后,我发现老师认为我的第一个例子对“嵌套 if 语句”有效。

采纳答案by BalusC

This is an if statement:

这是一个if 语句

if (condition1) {
    // ...
}

This is a nested if statement:

这是一个嵌套的 if 语句

if (condition1) {
    // ...
    if (condition2) {
        // ...
    }
    // ...
}

This is an if-else statement:

这是一个if-else 语句

if (condition1) {
    // ...
} else {
    // ...
}

This is a chained if-else statement:

这是一个链式 if-else 语句

if (condition1) {
    // ...
} else if (condition2) {
    // ...
}

回答by dcp

A nested if is like this:

嵌套的 if 是这样的:

if (...)
  if (...)
    ...;

The first example you gave is just if/else.

您给出的第一个示例只是if/else.

A good way to remember it is that the indentation increases with a nested if, but with if/elseit doesn't.

记住它的一个好方法是缩进随着嵌套增加if,但if/else没有。

回答by michael.bartnett

I understand it the same way you do.

我的理解和你一样。

If the implementation secretly only supports the "if" and "else" keywords, I suppose you could consider the "if" in "else if" to be an if statement nested inside an "else" statement. But I don't see this mattering all that much. The "else"'s scope contains only the "if" in this case, so it effectively behaves as a single scope.

如果实现秘密地只支持“if”和“else”关键字,我想您可以将“else if”中的“if”视为嵌套在“else”语句中的 if 语句。但我认为这并不重要。在这种情况下,“else”的范围仅包含“if”,因此它有效地表现为单个范围。

Is this mild curiosity, or are you running into scoping issues?

这是轻微的好奇心,还是您遇到了范围界定问题?

回答by Joel

You can nest an if without declaring an else, which seems to be your confusion.

您可以在不声明 else 的情况下嵌套 if,这似乎是您的困惑。

回答by Frank

I would assume all three nested ifs. As there is a general agreement on the other cases, I concentrate on your first case:

我会假设所有三个嵌套的 if。由于对其他案例有普遍共识,我专注于您的第一个案例:

This code is a nested if from my point of view, as a single ifis something like

从我的角度来看,此代码是嵌套的,因为单个代码if类似于

if(...) { } else { }

where the elsepart is optional. And you have effectively nested an if into the else part of the outer if else, even if the formatting (and maybe semantics) suggests it differently.

其中该else部分是可选的。并且您已经有效地将 if 嵌套到了外层的 else 部分if else,即使格式(可能还有语义)暗示了它的不同。

But in fact it boils down to the exact definition of "nested if", and I do not think there is a generally accepted one.

但实际上它归结为“嵌套如果”的确切定义,我认为没有普遍接受的定义。

Why do you ask? If this is something with regards to code efficiency, then that is not purely dependent on nesting level. If this is due to some guidelines with regard to if nesting levels, then the guideline should make clear how exactly it defines "nested if".

你为什么要问?如果这与代码效率有关,那么这并不完全取决于嵌套级别。如果这是由于有关 if 嵌套级别的一些准则,那么该准则应明确它如何准确定义“嵌套 if”。

回答by Matt Groff

The statement actually isan example of nesting in an irregular form.

该语句实际上一个以不规则形式嵌套的示例。

The form you have is:

你的表格是:

if(statement1 == true)
   code1...;
else if (statement2 == true)
   code2...;

This is equivalent to the following:

这等效于以下内容:

if (statement1 == false)
   if (statement2 == true)
      code2...;
else
   code1;

However, I think it's a poor programming practice to nest in this form. You should be able to rearrange code to fit a more traditional form.

但是,我认为以这种形式嵌套是一种糟糕的编程习惯。您应该能够重新排列代码以适应更传统的形式。

回答by JeremyP

Of course it's nested. Nesting has nothing to do with the way you format the code.

当然是嵌套的。嵌套与您格式化代码的方式无关。

if (...)
{
    // some code
}
else if (...)
{
    // some code
}
else
{
    // some code
}

is exactly equivalent to

完全等同于

if (...)
{
    // some code
}
else
{
    if (...)
    {
        // some code
    }
    else
    {
        // some code
    }
}

There's no 'else if' keyword in Java, the effect is achieved purely by a formatting convention which stops long chains of nested elses from drifting right across the screen.

Java 中没有“else if”关键字,这种效果纯粹是通过格式化约定来实现的,该约定阻止嵌套的其他长链在屏幕上漂移。

In case anybody needs persuading, here is the specification for an if statement: Java Language Specification section 14.9

如果有人需要说服,这里是 if 语句的规范: Java 语言规范第 14.9 节

回答by Dexygen

I don't consider the first form (if/else if/etcetera) to be "nested". Rather, its more akin to a switch block, which I also would not consider to be nested.

我不认为第一种形式(if/else if/etcetera)是“嵌套的”。相反,它更类似于 switch 块,我也不认为它是嵌套的。

回答by fastcodejava

If it is placed inside another ifblock it is a nested if. The first case is an if-elsestatement.

如果它被放置在另一个if块内,则它是一个嵌套的if. 第一种情况是if-else声明。

回答by Steve B.

Conceptually, for me the first form

从概念上讲,对我来说,第一种形式

if (cond1)
{
}
else if (cond2)
{
}
else
{
}

is not nested because all conditions are evaluated at the same logical level. In other words, I read this as "branch your action based on cond1:cond2:anything else".

不是嵌套的,因为所有条件都在相同的逻辑级别上进行评估。换句话说,我将其读作“根据 cond1:cond2:anything else 分支您的操作”。

if (cond1){ 
   if (cond2){ 
     if (cond3){ 

Is nested because you have conditions evaluated within other conditions , that is, dependenton previous conditions. If you want to be literal about it, the scope of cond2 in the second case is literally nested in the scope of cond1. This is not true in the first form.

嵌套是因为您在其他条件中评估了条件,即依赖于先前的条件。如果你想从字面上理解它,第二种情况下 cond2 的范围实际上嵌套在 cond1 的范围内。第一种形式并非如此。

I never thought this was controversial.

我从不认为这是有争议的。

edit: if you've ever had to read code like thisthe difference might seem less academic.

编辑:如果你曾经念想代码这样的差异可能显得不那么学术。