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

前端 未结 4 635
迷失自我
迷失自我 2021-01-26 18:54

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

         


        
4条回答
  •  逝去的感伤
    2021-01-26 19:10

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

提交回复
热议问题