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, whileb
false
) - print
b
(so printfalse
) - assign
b
not b
, is, opposite ofb
(so assignb
true
) - next iteration of loop,
b
true
,not b
condition fails , loop terminates
Comments
Post a Comment