Difference between pre-increment and post-increment in a loop?

后端 未结 22 1821
暗喜
暗喜 2020-11-21 23:41

Is there a difference in ++i and i++ in a for loop? Is it simply a syntax thing?

22条回答
  •  旧时难觅i
    2020-11-22 00:34

    I dont know for the other languages but in Java ++i is a prefix increment which means: increase i by 1 and then use the new value of i in the expression in which i resides, and i++ is a postfix increment which means the following: use the current value of i in the expression and then increase it by 1. Example:

    public static void main(String [] args){
    
        int a = 3;
        int b = 5;
        System.out.println(++a);
        System.out.println(b++);
        System.out.println(b);
    

    } and the output is:

    • 4
    • 5
    • 6

提交回复
热议问题