pre Decrement vs. post Decrement

后端 未结 2 1217
-上瘾入骨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: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.

提交回复
热议问题