英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论