You must also reevaluate the boolean
expression to set your value in the loop body for it to work, like
final int LIMIT = 5; // <-- try to avoid magic numbers.
boolean myBoolean = (value < LIMIT); // <-- assigns the result of the expression
// `value < LIMIT` to `myBoolean`.
while(myBoolean) {
System.out.println(value);
value = value + 1; // <-- value++;
myBoolean = (value < LIMIT);
}
without that last line updating myBoolean
, when myBoolean
is true
it will always be true
.
1
solved What is the reason behind this infinite loop?