1

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?

2 Answers 2

2

It should be &&, not ||, since you want the loop to loop as long as both ii<30 AND counter<=10.

Sign up to request clarification or add additional context in comments.

Comments

1

change while((!finish) || (counter <= 10)) to while((!finish) && (counter <= 10))

and it will work as you expect.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.