i— and i = i-1 not evaluating the same

后端 未结 4 1464
太阳男子
太阳男子 2021-01-29 04:48

I thought that i-- is a shorthand for i = i - 1, but I discovered that both evaluate different:

         


        
4条回答
  •  礼貌的吻别
    2021-01-29 05:19

    i-- is not the same as i=i-1, when used in a loop.

    i-- will be evaluated after the condition, but before the loop contents are run, whereas i=i-1 will be evaluated before the condition, so will evaluate to false.

    I guess in that respect --i is more similar to i=i-1, as it is also evaluated prior to the condition.

    You can think of

    while (i--) {...}
    

    As being equivalent to

    while (i = i - 1, i + 1) {...}
    

提交回复
热议问题