问题
I'm having trouble understanding how Post Increment (++
), Pre Increment (--
) and addition/subtraction work together in an example.
x++
means add 1 to the variable.
x--
means subtract 1 from the variable.
But I am confused with this example:
int x = 2, y = 3, z = 1;`
y++ + z-- + x++;
I assume this means 3(+1) + 1(-1) + 2(+1)
Which means the result should be 7.
But when I compile it, I get 6
. I don't understand.
int main() {
int x=2, y=3, z=1;
int result;
result = y++ + z-- + x++; //this returns 6
cout << result << endl;
return 0;
}
回答1:
result = y++ + z-- + x++;
3 1 2 = 6
if you perform this again
result1 = y++ + z-- + x++;
4 0 3 = 7
reason
operator++
returns the original value, before incrementing the variable.
and
++operator
returns the incremented value
--
is same as above just its decrement
回答2:
Because the postfix operator++
returns the original value, before incrementing the variable. The prefix operator++
increments the varialbe and returns a reference to it. The behaviour can be easily illustrated with an example:
#include <iostream>
int main()
{
int n = 1;
std::cout << n++ << "\n"; // prints 1
std::cout << n << "\n"; // prints 2
int m = 10;
std::cout << "\n";
std::cout << ++m << "\n"; // prints 11
std::cout << m << "\n"; // prints 11
}
回答3:
when you write x++
it uses the current value of x
and then increases it by one.
you want to write ++x
instead if you want to increase first.
回答4:
Pre-increment operator(++p)
first increase the value and assign it and post increment operator(p++)
first assign the value and then perform increment operation.Here all variable are post increment i.e it initially assign its value (on buffer) then increase (for y and x by 1) and decrease z by 1. i.e
initially assign 3 + 1 + 2 in buffer(addition is performed on buffer value) and then perform increment/decrements as x= 3,y=4 and z=0
for more information you can read answer on What is the correct answer for cout << c++ << c;? and Why are these constructs (using ++) undefined behavior? questions
回答5:
The position of ++
matter.
If
++
precedes the variable, e.g.++counter
, the value returned is the value in counter after it has been incremented. If++
follows the variable, e.g.counter++
, the value returned is the value in counter before it has been incremented.
(source)
回答6:
Postincrement/postdecrement will increment/decrement the variable, but evaluates to the variables 'previous' value.
So the expression result = y++ + z-- + x++
will act something like:
result = y + z + x; // result == 6
// perform the 'post' operations
y += 1;
z -= 1;
x += 1;
However, keep in mind that this is necessarily strictly how the compiler will perform the evaluation. For this expression, the results are well defined and you will end up as in the example. When using multiple increment/decrement operations in the same expression, it's easy to incorporate undefined behavior where you won't be able to expect anything in particular from the expression. See Why are these constructs (using ++) undefined behavior? for details.
回答7:
The reason is simple. The association principle is being used here, which calculates the values according to the precedence of operators.
ALSO X++
or X--
means... USE THEN CHANGE...
It will first use the value and the increment or decrement it.
If you want to get an output of 7
...
try this.. it might work...
result = ++y + z-- + x++;
回答8:
i++
/i--
are post increment and decrement of variable...
so in our expression it will take initial value for solving then inc/dec by 1.
int x = 2, y = 3, z = 1;
y++ + z-- + x++;
2 + 3 + 1 = 6
回答9:
You could also realise the different behaviour with a while loop:
#include <iostream>
void main{
int count = 0;
while (count < 5 )
{
std::cout << "counter is: " << count << std::endl
// replace this line with one of the lines below at a time
}
}
considering the following expressions for the increments of count
++count // the variable 'count' will grow from 0 to 4
count++ // ditto, weird as it seems; 'count' grows nonetheless
count = ++count // the old 'count' becomes 'count + 1'
count = count++ // endless loop: back to square zero time and time again
Likewise, you can get more insight with a do-while loop, with prefixed
count = 0;
do
{
std::cout << ++count << "\n";
} while (count < 5); // prints out 1 to 5
versus postfixed
count = 0;
do
{
std::cout << count++ << "\n";
} while (count < 5); // prints out 0 to 4
回答10:
Dim i As Integer = 10
Dim b As Integer
Dim c As Integer
Dim d As Integer
b = ++i
c = i + 1
d = b + c
Response.Write("<br/>")
Response.Write("The Value of ++i :-" & b)
Response.Write("<br/>")
Response.Write("The Value of i++ :-" & c)
Response.Write("<br/>")
Response.Write("Answer is " & d)
Output:
The Value of ++i :-10
The Value of i++ :-11
Answer is 21
Which means that ++var returns the original value and var++ returns incremented value.
来源:https://stackoverflow.com/questions/12551576/pre-increment-and-post-increment