vendredi 8 mai 2020

Why don't i get 'Unreachable code' error when i use 'if' statement in Java? [duplicate]

When i use this code, for obvious reasons, i get compilation error "Unreachable code"

public static void main (String... args){

  while (true);

  System.out.println("test"); //Compilation error: Unreachable code

}

But when i use 'if' statement before 'while' statement, i don't get compilation error.

public static void main (String... args){

  if (true) while (true);

  System.out.println("test"); //Here i only get a warning, but no execution or compilation errors

}

Why if i use the 'if' statement i don't get errors, even though the infinite loop is explicit?

Aucun commentaire:

Enregistrer un commentaire