How is the time when the cycle knows when to fail?

I read about how to program network sockets and ran through 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 the crash? In other words, how does the while(true) work? I guess I don’t understand: "And what is the truth?"

+4
source share
2 answers

The important line is here:

 if (i == -1) break; 

break will exit the current loop when i == -1 . Without this line, it will be an endless loop.

+7
source

Shafik said that. I just add that while(true) means while (true == true) , because while the loop is checking the boolean condition. This particular condition is always true, and the loop will be infinite unless you define a break or return condition somewhere in it.

0
source

Source: https://habr.com/ru/post/1485130/


All Articles