The auto-increment operator will add 1 to the operand, and the auto-decrement operator will decrement the operand by 1, and you can control whether the effect acts before or after the operation, which is very convenient.
The auto-increment and self-decrement operator exists in C/C /C#/Java/Python
and other high-level languages. Its function is Add (or subtract) the value of the variable by one before (prefixed increment and decrement operator) or after (postfixed increment and decrement operator) the end of the operation.
Compared with the = and -= operators in these languages, the increment operator is more concise and can control whether the effect acts before or after the operation, which is very convenient.
Auto-increment operator
Increases the value of the operand by 1, and its operand must be a variable lvalue (can be simply understood as a variable). Eric thinks everyone will have no doubts about the fact that self-increment means adding 1.
can be placed in front of the operand or behind it, such as:
i;
i ;
i
means that i increases by 1 before participating in other operations; and i
means that after i participates in operations, the value of i increases by 1.
Auto-decrement operator--
is similar, except that it changes addition to subtraction, so I won’t repeat it.
Let’s use some examples to deeply understand the characteristics of the auto-increment operator and the self-understanding of the auto-decrement operator.
Example:
int i=3; int j=4; i++; ++j; printf("%d, %d\n", i, j);
In this regard, Eric thinks that everyone will not What confusion will there be? The result is 4, 5; let’s make a small change:
int i=3; int j=4; int a = i++; int b = ++j; printf("%d, %d\n", a, b);
What is the result? Here the difference between
preposition and postposition begins to show, and the result is 3,5. Combined with this example, let's go back and understand "
Prefix: i increases by 1 before participating in other operations;
Postfix: after i participates in the operation, the value of i increases by 1. ". Obviously, a = i; because the assignment operation is performed first and then incremented, the result is a=3, i=4; and b = j;
because it is incremented first and then assigned, so b, j are all 5.
The above is the detailed content of Detailed explanation of auto-increment and auto-decrement operators. For more information, please follow other related articles on the PHP Chinese website!