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

前端 未结 4 524
渐次进展
渐次进展 2021-01-26 18:32

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

         


        
4条回答
  •  余生分开走
    2021-01-26 19:25

    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) {...}
    

提交回复
热议问题