在Java中真的存在`else if`吗?

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

Do the `else if` really exist in java?

问题

else if在Java中真的存在吗?还是只是紧接着一个else后面再跟一个if

Java允许您在只有一个语句的情况下省略大括号。现在这意味着这个:

if(..){

}
else{
   if(...){

   }
}

将和这个等效:

if(...)

else
    if(...)

如果去掉空格,则为:

if(...)

else if(...)

这正确吗?

英文:

Is the else if really exist in java? or it is just an else followed by an if?

Java will let you type blocks without the brackets if there's just one statement below. Now this means this

if(..){

}
else{
   if(...){

   }
}

would be the same as this...

if(...)

else
    if(...)

and if you eat the white space would be

if(...)

else if(...)

is this correct?

答案1

得分: 11

你可以在JLS中查看语法:

语句:
    [...]
    if ParExpression 语句 [else 语句] 
    [...]

换句话说,else if 将被解析为一个新的 if 语句,而不是作为一个单元连在一起。

英文:

You can have a look at the grammar in the JLS:

Statement:
    [...]
    if ParExpression Statement [else Statement] 
    [...]

else if will in other words just be parsed as else followed by a new if statement, and not together as a unit.

huangapple
  • 本文由 发表于 2020年9月29日 05:38:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/64109969.html
匿名

发表评论

匿名网友

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

确定