来自while循环Java的意外输出

Und*_*Dog 1 java

这是我无法理解的行为.

class loop1 {
    public static void main(String args[]) {
        int i = 10;

        do
            while(i++ < 15) {
                System.out.println(i);
                i = i + 20;
                System.out.println(i);
            } 
        while(i<2);

        System.out.println(i);    
    }

}
Run Code Online (Sandbox Code Playgroud)

我希望它能打印出来

11
31
31
Run Code Online (Sandbox Code Playgroud)

但它打印

11
31
32
Run Code Online (Sandbox Code Playgroud)

我无法理解为什么这个"32"出现在输出中.

这是我对流程的理解

  • 我= 10
  • 在while循环中,由于一元增量,它变为11,所以这解释了第一个输出
  • 11增加到31(+20)
  • 然后31 <15应该失败(在下一次迭代期间),所以它应该进入最后一个打印语句并打印31,但它改为打印32.

有人能告诉我我错过了什么吗?

Kev*_*sox 5

在第一次while循环的最终评估期间,即使由于条件失败而未执行循环,i++仍然会增加i循环.

class loop1 {
    public static void main(String args[]) {
        //1.  i = 10
        int i = 10;

        do 
            // 2. while loop condition = (10 < 15), i = 11
            // 6. while loop condition = (31 < 15), i = 32
            while(i++ < 15) {
                System.out.println(i);  //3. prints 11
                i = i + 20; //4. i = 31
                System.out.println(i);  //5. prints 31
            } 

        while(i<2); //this really has no effect on the codes execution, given i values

        System.out.println(i); //7.  Prints 32
    }

}
Run Code Online (Sandbox Code Playgroud)