pre Decrement vs. post Decrement

后端 未结 2 1211
-上瘾入骨i
-上瘾入骨i 2020-12-06 23:55

When should I use pre decrement and when to use post decrement?

and for the following code snippet, should I use pre or post decrement.

static privat         


        
相关标签:
2条回答
  • 2020-12-07 00:19

    you should have ++i; (not that it matters), and should have tempCounter-- Otherwise you will miss the "first" index of charArr

    0 讨论(0)
  • 2020-12-07 00:24

    You use pre-decrement if you want to decrement the variable before the value is passed on to the remaining expression. On the other hand, a post-decrement evaluates the expression before the variable is decremented:

    int i = 100, x;
    x = --i;                // both are 99
    

    and

    int i = 100, x;
    x = i--;                // x = 100, i = 99
    

    The same obviously is true for increments.

    0 讨论(0)
提交回复
热议问题