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

后端 未结 27 2515
-上瘾入骨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 06:58

    From the Java Language Specification chapter on for loops:

    BasicForStatement:

        for ( ForInit ; Expression ; ForUpdate ) Statement
    

    ... if the ForUpdate part is present, the expressions are evaluated in sequence from left to right; their values, if any, are discarded. ... If the ForUpdate part is not present, no action is taken.

    (highlight is mine).

提交回复
热议问题