0

This does not compile, giving an unreachable statement error:

  while(true)
  {
  }
  return null;

while this does not have any compilation issues though we know that it is equivalent to the snippet above:

  while(true)
  {
   if(4>5)
       break;
  }
  return null;

Does the compiler simply search for a break statement and think that there's some way that the loop may terminate and so does not worry about it?

rahs
  • 1,759
  • 2
  • 15
  • 31

1 Answers1

-1

The compiler does not evaluate expressions within conditional statements. Even if you write:

while (true) {
    if (false) {
        break;
    }
}

it would not be detected as an infinite loop.

DudeDoesThings
  • 729
  • 3
  • 13
  • Of course it evaluates them. The example in the question and yours simply introduce some dead code because of the constant expression in the condition. – Sotirios Delimanolis Oct 02 '18 at 00:58