嵌套的if-else行为没有大括号 [英] Nested if-else behaviour without braces
本文介绍了嵌套的if-else行为没有大括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
考虑以下未格式化的嵌套 if-else
Java代码
Consider the following unformatted nested if-else
Java code
if (condition 1)
if (condition 2)
action 1;
else
action 2;
我的问题是:根据Java语言规范,如果 else
分支申请?
My question is: according to the Java language specifications, to what if does the else
branch apply?
通过手工重新格式化和添加大括号,这两个中的哪一个是正确的?
By hand-reformatting and adding braces, which of these two is correct?
第1区:
if (condition 1) {
if (condition 2) {
action 1;
} else
action 2;
}
}
第2块:
if (condition 1) {
if (condition 2) {
action 1;
}
}
else {
action 2;
}
推荐答案
来自文档:
Java编程语言,如C和C ++以及它们之前的许多编程语言,任意地规定else子句属于它可能属于的最内层。
The Java programming language, like C and C++ and many programming languages before them, arbitrarily decrees that an else clause belongs to the innermost if to which it might possibly belong.
这篇关于嵌套的if-else行为没有大括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文