Why doesn't changing the pre to the post increment at the iteration part of a for loop make a difference?

后端 未结 27 2460
-上瘾入骨i
-上瘾入骨i 2020-11-30 06:22

Why does this

 int x = 2;
    for (int y =2; y>0;y--){
        System.out.println(x + \" \"+ y + \" \");
        x++;
    }

prints the s

27条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-30 07:16

    Those two cases are equivalent because the value of i is compared after the increment statement is done. However, if you did

    if (i++ < 3) 
    

    versus

    if (++i < 3)
    

    you'd have to worry about the order of things.

    And if you did

    i = ++i + i++;
    

    then you're just nuts.

提交回复
热议问题