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

前端 未结 4 623
迷失自我
迷失自我 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:11

    The i-- will be evaluated only after the loop condition is evaluated but before the statements within the loop. This is the decrement postfix operator. That means that initially the evaluation of the condition in the loop condition would be true. This will fire the execution of the statements in the loop's body. When the statements in the loop's body will be executed, since the i had be decremented by one, it would be equal to 0. Hence the evaluation of the loop condition would be false and the statements in the loop's body will not be executed again.

提交回复
热议问题