lundi 8 juin 2015

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

This question already has an answer here:

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?

Aucun commentaire:

Enregistrer un commentaire