JAVA BNF "no short if" - 这是什么意思?
JAVA BNF "no short if" - what does this mean?
基本上是标题。
查看 Java BNF,我看到 "no short if" 例如:
<statement no short if> ::= <statement without trailing substatement> | <labeled statement no short if> | <if then else statement no short if> | <while statement no short if> | <for statement no short if>
"no short if" 是什么意思?我看到 "NoShortIf" 突然出现在我的演讲幻灯片中,但没有解释它的含义。
答案在上面@Andy Turner 的评论中提供的 link 中:
Statements are thus grammatically divided into two categories: those
that might end in an if statement that has no else clause (a "short if
statement") and those that definitely do not.
Only statements that definitely do not end in a short if statement may
appear as an immediate substatement before the keyword else in an if
statement that does have an else clause.
This simple rule prevents the "dangling else" problem. The execution
behavior of a statement with the "no short if" restriction is
identical to the execution behavior of the same kind of statement
without the "no short if" restriction; the distinction is drawn purely
to resolve the syntactic difficulty.
"short if" 是没有 else 的 if 语句。
"Short ifs"在某些情况下不允许使用,以消除歧义。
以下有效Java。没有"short ifs"也没有歧义
boolean flag = false;
if (x > 0)
if (x > 10)
flag = true;
else
flag = false;
else
flag = true;
以下也是有效的 java 代码,但如果没有 "short if" 规则,则 else 属于哪个存在歧义。
if (x > 0) if (x < 10) flag = true; else flag = false;
以下Java语言规则
IfThenStatement:
if ( Expression ) Statement
IfThenElseStatement:
if ( Expression ) StatementNoShortIf else Statement
暗示上面代码的意思是
if (x > 0)
if (x < 10)
flag = true;
else
flag = false;
即else属于内层if语句
让我们在 Java 中进行测试以确保
static Boolean shorty (int x) {
Boolean flag = null;
if (x > 0) if (x < 10) flag = true; else flag = false;
return flag;
}
public static void main(String[] args) {
System.out.println(shorty(-1));
System.out.println(shorty(5));
System.out.println(shorty(20));
}
输出为
null
true
false
基本上是标题。 查看 Java BNF,我看到 "no short if" 例如:
<statement no short if> ::= <statement without trailing substatement> | <labeled statement no short if> | <if then else statement no short if> | <while statement no short if> | <for statement no short if>
"no short if" 是什么意思?我看到 "NoShortIf" 突然出现在我的演讲幻灯片中,但没有解释它的含义。
答案在上面@Andy Turner 的评论中提供的 link 中:
Statements are thus grammatically divided into two categories: those that might end in an if statement that has no else clause (a "short if statement") and those that definitely do not.
Only statements that definitely do not end in a short if statement may appear as an immediate substatement before the keyword else in an if statement that does have an else clause.
This simple rule prevents the "dangling else" problem. The execution behavior of a statement with the "no short if" restriction is identical to the execution behavior of the same kind of statement without the "no short if" restriction; the distinction is drawn purely to resolve the syntactic difficulty.
"short if" 是没有 else 的 if 语句。
"Short ifs"在某些情况下不允许使用,以消除歧义。
以下有效Java。没有"short ifs"也没有歧义
boolean flag = false;
if (x > 0)
if (x > 10)
flag = true;
else
flag = false;
else
flag = true;
以下也是有效的 java 代码,但如果没有 "short if" 规则,则 else 属于哪个存在歧义。
if (x > 0) if (x < 10) flag = true; else flag = false;
以下Java语言规则
IfThenStatement:
if ( Expression ) Statement
IfThenElseStatement:
if ( Expression ) StatementNoShortIf else Statement
暗示上面代码的意思是
if (x > 0)
if (x < 10)
flag = true;
else
flag = false;
即else属于内层if语句
让我们在 Java 中进行测试以确保
static Boolean shorty (int x) {
Boolean flag = null;
if (x > 0) if (x < 10) flag = true; else flag = false;
return flag;
}
public static void main(String[] args) {
System.out.println(shorty(-1));
System.out.println(shorty(5));
System.out.println(shorty(20));
}
输出为
null
true
false