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

后端 未结 22 2122
暗喜
暗喜 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:13

    They both increment the number. ++i is equivalent to i = i + 1.

    i++ and ++i are very similar but not exactly the same. Both increment the number, but ++i increments the number before the current expression is evaluated, whereas i++ increments the number after the expression is evaluated.

    int i = 3;
    int a = i++; // a = 3, i = 4
    int b = ++a; // b = 4, a = 
    

    Check this link.

提交回复
热议问题