Prathima gaitonde wrote:Hi All, I have the same problem, I cant get the concept of reachable code and unreachable code,
as Roel said:
if(2<1){dosomething} is fine!!!
while(2<1){dosomething} compiler error???
If you have a while statement with a condition which always evaluates to
false (2 can never be smaller than 1), the body of the loop is unreachable and you'll get a compiler error.
These all don't compile:
Why is this not with an
if-statement. I can illustrate with an easy example. Let's assume I have a
boolean flag
DEBUG. When I'm developing my application, I set this value to
true and I get a bunch of messages sent to the console for debugging purposes. When this application is released, I'll set this value to
false. Disclaimer: when you develop a real application you should use a logging framework instead of this approach
So no problem here: the print-statement is always reachable. Now it's time to release the application and set the flag
DEBUG to
false.
This code would not compile anymore, because the print-statement would now be unreachable. Uh-oh! It was compiling in development and
test, but not anymore in production
That's why there is a difference between an
if-statement and a loop (
while,
for,
do while)
Hope it helps!
Kind regards,
Roel