I was reading about how to program network sockets and ran across this piece of code:
try {
while (true) { // This is the line in question
int i = in.read( );
if (i == -1) break;
System.out.write(i);
}
}
catch (SocketException e) {
// output thread closed the socket
}
catch (IOException e) {
System.err.println(e);
}
How does the second line know when to fail? In other words, how does the while(true)
loop work? I guess I don't understand 'While what is true?'
The important line here is:
if (i == -1) break;
The break
will exit the current loop when i == -1
. Without this line it would be an infinite loop.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With