What is the difference between prefix and postfix operators?

前端 未结 13 1493
天命终不由人
天命终不由人 2020-11-22 08:01

The following code prints a value of 9. Why? Here return(i++) will return a value of 11 and due to --i the value should be 10 itself, can anyone ex

相关标签:
13条回答
  • 2020-11-22 08:54

    In fact return (i++) will only return 10.

    The ++ and -- operators can be placed before or after the variable, with different effects. If they are before, then they will be processed and returned and essentially treated just like (i-1) or (i+1), but if you place the ++ or -- after the i, then the return is essentailly

    return i;
    i + 1;
    

    So it will return 10 and never increment it.

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