问题
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