Java while loop boolean evaluation -
i not sure if understand loop
boolean b = false; while(!b) { system.out.println(b); b = !b; } it returns false, loop executed once
but while(!b) set b= true ? !b = !false , b printed out?
the while (!b) condition not set b true.
the b = !b statement does.
that's why loop executes once.
translation in pseudo-code:
- while
not b(that is, whilebfalse) - print
b(so printfalse) - assign
bnot b, is, opposite ofb(so assignbtrue) - next iteration of loop,
btrue,not bcondition fails , loop terminates
Comments
Post a Comment