与else语句不一致? [英] Inconsistency with else statements?

查看:163
本文介绍了与else语句不一致?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我之前问题的修订版。

当使用else-if作为快捷方式时,我不确定为什么,语法方面,它执行与嵌套if-else语句相同的功能。假设您有以下内容:

When using else-if as a shortcut, I'm not exactly sure why, syntax wise, it performs the same functionality as nesting if-else statements. Let's say you have the following:

int score = 80;
if(score < 80) {
  System.out.println("C Grade");
} else if(score < 90) {
  System.out.println("B Grade");
} else if(score < 100) {
  System.out.println("A Grade");
}

如果我添加一些缩进,我会得到类似的东西:

If I add some indentation, I get something similar to:

if(score < 80) {
  System.out.println("C Grade");
} else 
  if(score < 90) {
    System.out.println("B Grade");
  } else 
    if(score < 100) {
      System.out.println("A Grade");
    }

这与编写嵌套的if-else语句的结果相同:

This evaluates the same as writing nested if-else statements as such:

if(score < 80) {
    System.out.println("C Grade");
} else { 
    if(score < 90) {
        System.out.println("B Grade");
    } else { 
        if(score < 100) {
        System.out.println("A Grade");
        }
    }
}

我看到一个主要的区别,然而。在第三个代码片段中,else语句有括号,其余的if-else语句嵌套在这些括号中。在第一种情况下,没有封装括号。那么为什么第一个想法是else-if work,如果我在嵌套if-else语句时似乎需要括号?我认为缺少带有控制流语句的括号,例如if,else,while等,导致编译器只评估以下行,例如:

I see a major difference, however. In the third code snippet, the "else" statement has brackets, and the remaining if-else statements are nested inside those brackets. While in the first scenario, there are no encapsulating brackets. So why does the first idea of else-if work, if I seemingly need to have brackets when nesting if-else statements? I thought that a lack of brackets with control flow statements such as if, else, while, etc. cause the compiler to only evaluate the following line, such as:

if(condition)
    System.out.println("A");
    System.out.println("B");
//only prints "A"


推荐答案

首先,请注意如果 else 始终适用于它们后面的单个语句。您可以使用 {} 将多个语句分组到单个复合语句中,但条件则适用于单个 {...} 声明。

First off, note that if and else always apply to the single statement that follows them. You can use { } to group multiple statements into a single compound statement, but the conditional then applies to the single { ... } statement.

考虑这两个等价的陈述:

Consider these two equivalent statements:

if (foo) {
  bar();
}

if (foo)
  bar();

第一个如果适用于 {} block(包含 bar(); ),而在第二个它直接应用于 bar(); 声明。可能不会立即显而易见的是 {} 并不是创建复合语句的唯一方法。实际上,如果是另一个复合表达式,因此它可以是另一个的直接子节点,如果 else 声明。例如:

In the first the if applies to the { } block (which contains bar();) while in the second it applies directly to the bar(); statement. What may not be immediately apparent is that { } is not the only way to create a compound statement. In fact, if is another compound expression, hence it can be the direct child of another if or else statement. For instance:

if (foo)
  if (bar) {
    baz();
  }

完全有效(如果难以阅读)并且相当于:

Is perfectly valid (if hard to read) and is equivalent to:

if (foo) {
  if (bar) {
    baz();
  }
}

else - 如果前面的条件为假,则将应用后面的任何语句,即使该语句本身是复合语句!

The same concept is true of else - whatever statement follows it will be applied if the preceding conditional is false, even if that statement is itself a compound statement!

if (foo) {
  bar();
}
else
  if (baz) {
    biff();
  }

else 适用整个 if(baz)块,因为它只是一个(复合)语句,所以不需要额外的 {} 阻止。

The else applies to the whole if (baz) block, and since it's only a single (compound) statement there's no need for an additional { } block.

确切的语法在JLS§19,以及 if 语句的不同形式的语法详见< a href =https://docs.oracle.com/javase/specs/jls/se8/html/jls-14.html#jls-14.9\"rel =nofollow noreferrer>JLS§14.9。请注意,如果没有提及 else,因为它不是语言的正式部分,只是语法的(故意)后果。

The exact grammar is specified in JLS §19, and the grammar of the different forms of the if statement are detailed in JLS §14.9. Notice that there's no mention of else if because it's not a formal part of the language, simply a (intentional) consequence of the grammar.

所有这些都说明了真正的原因我们没有像你描述的那样格式化代码,这只是因为它更难读书。将 else 视为单个关键字并相应地缩进代码会更简单。其他一些语言(例如 Python )使用单个关键字,如 elif 简化语法,但这不是必需的。

All of that said, the real reason we don't format the code the way you're describing is simply because it's harder to read. It's much simpler to think of else if as a single keyword and indent your code accordingly. Some other languages (e.g. Python) use a single keyword like elif to simplify the grammar, but it's not really necessary.

这篇关于与else语句不一致?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆