简体   繁体   中英

Java while loop updating the condition?

Is there a way to update the condition in a while loop?
In the below code, updating j if someCondition has been met?
So that that the initial loop doesn't loop for 20 times,
but rather it loops for 20+n times whenever someCondition.

public class foo {
    public static void main(String[] args) {
        int i = 0;
        int j = 20;
        while(i < j) {
            //do something
            i++
            if(someCondition) {
                j++;    
            }
        }
    }
}

yes you can. just try it and you will see that you can. even more, you can change i inside for(int i=0; i<n; i++) , there is no problem with this kind of changes.

用 do/while 循环试试,它在执行代码后比较值

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM