What happens when “if” statement is ended with a semi colon [duplicate]

China☆狼群 提交于 2019-12-24 13:04:58

问题


In a program, I needed an if statement and by mistake, I put semicolon at the end of the statement. However, there were neither compile-time error nor run-time error. I tried to figure out what this code means but hopeless.

if (i == 10); 
{
    System.out.println("It is here");
    break;
} 

If you enlighten me on this topic, that will be appreciated.


回答1:


The if statement has nothing to do with the following block:

if (i == 10);

This is a valid statement as ; denotes an empty statement: if (i == 10) then do nothing.

{
    System.out.println("It is here");
    break;
}

This is a valid code block. It is syntactically correct although it does not help a lot in this case. This block will be executed in all cases and is not affected by the if statement above.



来源:https://stackoverflow.com/questions/32508731/what-happens-when-if-statement-is-ended-with-a-semi-colon

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!