Here is my question: normal while loop looks like this -
while(condition){statements}
and statements execute until condition becomes false
and the loop is over. I'm interested in logic of this kind of statement:
while((a=b.readLine) != null) { ... }
It's used in client - server communication in my case. The condition is sometimes true, sometimes isn't, but loop looks like it's testing the condition forever and when true, statements in {}
execute. It looks to me that loop waits for the condition to be true and then runs statements. Is it somehow linked to the way BufferedReader
and InputStreamReader
work or what? Also, it seems this loop is never over, it just waits for a condition to be true
and then runs statements, and then again waits for a condition to be true
, etc. I would be thankful for any clarification.
The following code snippet
String a;
while((a = b.readLine()) != null) { // a = b.readLine() -> a -> a != null
...
}
is equivalent to
String a = b.readLine();
while(a != null) {
...
a = b.readLine();
}
but the first way is more simple, readable, and Java allows using assignment operators here because they return the value of a renewed variable.