Reputation: 1456
I have the following Java while loop:
boolean finish = false;
int ii = 0;
int counter = 0;
while((!finish) || (counter <= 10)) {
ii++;
if(ii<30) {
System.out.println(ii + " -- " + counter);
}else {
finish=true;
}
counter++;
}
I want the loop to add one to ii
until it reaches 30 or the counter
reaches 10. Running this code ignores the condition of counter and continues until ii
reaches 30. I expect it to stop when counter
reaches 10.
How can I fix that?
Upvotes: 1
Views: 33
Reputation: 100
change while((!finish) || (counter <= 10)) to while((!finish) && (counter <= 10))
and it will work as you expect.
Upvotes: 1
Reputation: 5387
It should be &&, not ||, since you want the loop to loop as long as both ii<30 AND counter<=10.
Upvotes: 2