I’m curious whether you are doing this for doing questions or whether you have seen the author using this writing method in the code of an open source project. If it is for doing questions or written tests, I suggest you not consider such a school or company. Because this question is meaningless. , swift3 even removes the ++ operator. Life is short, and it is not worth wasting time on confusing or error-prone syntax features.
The assignment expression has a return value, which is referred to as the value of the expression
z=z=z++;
It is equivalent to assigning the value of the expression "z=z++" to z, and the value of "z=z++" is equal to "z++". "z++" first uses the current value of z and then increments it
Post-increment: An expression like n++ will return a copy of the original value of n, and then the original value of n++ n = n++. The post-increment operator has a higher priority than assignment So the right side of = will First increment n and return a copy of the original value of n Then perform an assignment operation to assign the original value of n to n, so the value of n remains unchanged
z++ is an expression, and the result of the expression is still z, so z=z++ is equivalent to z=z which does nothing. You need to understand that the logic of z++ is to return the variable first value before incrementing.
Or you can use ++z, the pre-increment is to increment yourself first, and then return the result after the auto-increment
I’m curious whether you are doing this for doing questions or whether you have seen the author using this writing method in the code of an open source project. If it is for doing questions or written tests, I suggest you not consider such a school or company. Because this question is meaningless. , swift3 even removes the ++ operator. Life is short, and it is not worth wasting time on confusing or error-prone syntax features.
Two points:
a = a++ is assigned first and then incremented
The assignment expression has a return value, which is referred to as the value of the expression
It is equivalent to assigning the value of the expression "z=z++" to z, and the value of "z=z++" is equal to "z++". "z++" first uses the current value of z and then increments it
Post-increment: An expression like
n++ will return a copy of the original value of n, and then the original value of n++
n = n++. The post-increment operator has a higher priority than assignment
So the right side of = will First increment n and return a copy of the original value of n
Then perform an assignment operation to assign the original value of n to n, so the value of n remains unchanged
z++
is an expression, and the result of the expression is stillz
, soz=z++
is equivalent toz=z
which does nothing.You need to understand that the logic of
z++
is to return the variable first value before incrementing.Or you can use
++z
, the pre-increment is to increment yourself first, and then return the result after the auto-incrementis equivalent to
++ and = problems with the order of operations. It is recommended to take a look at the priority of operation and assignment