On line 2 in above code, if I replace condition with false, compiler throws Unreachable statement error but if I do the same thing on line 1, no error is thrown. Can anyone explain me why?
I find SUN's explanation on what's reachable accurate but not easy to remember.
So may be if I give you my idea, it might be simpler:
For all loops if the boolean constant is known to be false at compile time, then you get a compilation error.
However, the if statement is just like asser(). It MIGHT be used for
testing situations that are not supposed to happen. Therefore, you will not get a compliation error.
That's the logic I use and it makes sense to me. It seems it explains most unreachable code problems concerning loops and ifs.