2

I have a while loop (shown below) that continually reads from a file until EOF is reached. I am supposed to write a loop invariant for any non-trivial loop. Is this a trivial loop? If not, what would be a loop invariant for this while loop? I have never written invariants before.

while (line != null) {
    System.out.println(line);
    line = reader.readLine();
}
Alex Parker
  • 1,533
  • 3
  • 16
  • 38
  • 1
    Are you using some formalism like Hoare logic? http://en.wikipedia.org/wiki/Hoare_logic – Codor Jan 22 '15 at 12:40

1 Answers1

-1

You can use a fileReader:

while ((line = fileReader.hasNextLine()) != null) {
    String line = fileReader.nextLine(); 
    System.out.println(line);
}

=D

Kevin F
  • 169
  • 7
  • 17