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

后端 未结 4 1466
太阳男子
太阳男子 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:15

    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.

提交回复
热议问题