How does a for loop check its conditions in Java?

前端 未结 5 1797
小鲜肉
小鲜肉 2021-01-04 19:32

My question has to do with the order in which java checks the conditions of a for loop when there is a print statement in the \"conditions\" of the loop. It

5条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-04 20:24

    for (  ;  ;  ) {
         
    }
    

    is equivalent to something like:

    
    while (  ) {
        
        
    }
    

    So you have:

    i = -1;
    while ( i < n ) {
        i++;
        System.out.print(i + " ");
    }
    

    The "problem" in your test question is that the types of statements usually put in the and portions are switched.

提交回复
热议问题