An issue in using while loop with multiple conditions

Adam Amin :

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?

Thiagesh thg :

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

and it will work as you expect.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=401252&siteId=1