Why does compiler recognize while(true) at compile time but not if(true)

107 views Asked by At

The below code gives compile time error - Unreachable statement

while(true)
  return;
return;  // Unreachable statement

But, the following code does not give any error:

if(true)
   return;
return;  // Reachable, No Error

I am unable to understand why the compiler recognizes that in case of while, the 2nd return statement will never be reached, but it does not recognize the same in case of if?

0

There are 0 answers