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

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

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

22条回答
  •  长情又很酷
    2020-11-22 00:14

    There is no actual difference in both cases 'i' will be incremented by 1.

    But there is a difference when you use it in an expression, for example:

    int i = 1;
    int a = ++i;
    // i is incremented by one and then assigned to a.
    // Both i and a are now 2.
    int b = i++;
    // i is assigned to b and then incremented by one.
    // b is now 2, and i is now 3
    

提交回复
热议问题