if/else statement in Java

java

Solution

if(a==100); //Expected a compile error but did not get one.

No compiler error as this is assumed to be an empty if block as it is terminated by semicolon `(;)`

if(a == 101) //Compiler complains here...

This is an incomplete if block, there should be at least one statement in if or else it should be terminated by `semicolon(;)` as above. As the if is not complete so else will also not make any sense to compiler.

Problem

``` class if1 { public static void main(String args[]) { int a = 100; if(a==100); //Expected a compile error but did not get one. } } ``` I expected the compiler to issue me an error but surprisingly it compiled just fine. What is the rationale for a compiler to ignore whether the "if" statement has any statements to process or not. Why does it not throw an error like in the below case ? ``` class if2 { public static void main(String args[]) { int a = 100; if(a == 101) //Compiler complains here... else { System.out.println("in else"); } } } ``` In the above statement, the compile complains that "if" clause does not have anything to process. Can someone tell me why ?

Original source