This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What is x after “x = x++”?
In a loop I have:
int x = 0;
while(int x < 10){
x = x++;
}
Why does this not work?
Change x = x++ to just x++. x++ is unary operation and you don't need to use the assignment operation.
Post increment operator uses the value first and then increments.
x=x++;
Here, we assign X to X (zero to zero) and then we increment X to 1 but we never assign it to anything.
you can change to
X=++X;
and this should give you what you want.
Well... try this instead.
int x = 0;
while(int x < 10){
x++;
}
Try converting
x = x++;
to
y = x++; // i.e. y = x, then increment x
x = y;
to understand it.
Related
This question already has answers here:
What are the rules for evaluation order in Java?
(5 answers)
Closed 4 years ago.
For the next code, what it's z? (Java)
int x = 5;
int y = 10;
int z =++x*y--;
The order of priority is: y--, ++x, *, =.
( https://introcs.cs.princeton.edu/java/11precedence/ )
Why after run the code, z = 60 ?
The ++ operator is evaluated before the expression.
i.e.:
int x = 10;
int y = ++x; //y = 11
int z = x ++; // z = 11;
y-- is higher up on the list from your source. However, when the post-decrement happens, it happens after the whole evaluation.
So if you print y after getting the value of z, it will be 9.
And the pre-increment happens first, so ++x becomes 6 within that statement (and obviously multiplies by 10).
See an example in the docs.
I read a few post regarding unary operator:
What is the Difference between postfix and unary and additive in java
"C - C++" joke about postfix/prefix operation ordering
And a few more.
However, I still don't understand exactly when the value is changed.
For example:
int x = 1;
x = x++;
System.out.print("x = x++ ==> ");
System.out.print(" x = " + x);
System.out.println();
int x = 1;
x = x++ + x++;
System.out.print("x = x++ + x++ ==> ");
System.out.print(" x = " + x);
System.out.println();
The output is:
x = x++ ==> x = 1
x = x++ + x++ ==> x = 3
So in the first block x is assigned to x and afterwards incremented, but the value is never used, otherwise the output would have been x = 2.
In the second block, if I understand correctly, the first x++ is evaluated before the assignment and the second x++ is evaluated afterwards but is never used.
If in the second block both x++ would have been evaluated after the assignment but never used, the output would have been x = 2. If both have been used, the output would have been x = 4.
My IDE also indicated that the first x++ is used, but the second is not used:
So to conclude - I'm still confused about when and how exactly the increment is done.
At the line
x = x++ + x++;
Assuming x = 1, the first x++ returns "1" as the value, and then it increments x to 2. So basically, it's assigning the old value back to x.
The second x++ does the same; it returns the value of x, which is now 2, and only then increments its value to 3 - that value, is not used.
Your code is equivalent to:
tmp = x;
x = x + 1;
tmp2 = x;
x = x + 1; // not used
x = tmp + tmp2;
Links that may help you:
JSL - 15.14.2. Postfix Increment Operator ++
JLS - 15.15.1. Prefix Increment Operator ++
What is x after “x = x++”?
This question already has answers here:
Java - Order of Operations - Using Two Assignment Operators in a Single Line
(4 answers)
Closed 7 years ago.
This is allowed by the Java compiler, what is it doing?
int x = x = 1;
I realize that x is assigned to x, but how can it have two =s?
x = 1 both assigns the value 1 to x and also 'returns' 1, it allows for things like this:
while ((line = reader.readLine()) != null)
Read assignment statement from right to left:
Acording to Assignment Operators
There are 12 assignment operators; all are syntactically
right-associative (they group right-to-left). Thus, a=b=c means
a=(b=c), which assigns the value of c to b and then assigns the value
of b to a.
So,
int x = x = 1;
is the same as
x = (x = 1);
then
x = 1; x = x;
int x puts x on the stack.
The right hand part x = 1 assigns 1 to x. But this is an expression with value 1.
Finally this is re-assigned to x.
This question already has answers here:
What is x after "x = x++"?
(18 answers)
Why does this go into an infinite loop?
(26 answers)
Closed 9 years ago.
Consider the following code :
public class Game {
public static void main(String[] args) {
int x = 0;
while (x < 1) {
x = x++;
}
}
}
With my compiler this code goes into infinite loop , but why ?
First , we place the value of x into x , and only then we add 1 to x , and afterwards I would expect that 1 < 1 would be false , and the while loop would terminate ...
But no , that doesn't happen .
So what might be the reason for the infinite loop ?
The expression x++ is a post-increment, meaning that it the value of the expression is the old value, 0, which is then assigned back to x, so x is always 0 after x = x++;.
To break out of the infinite loop, don't assign it back to x, leaving the post-increment value of x, 1, intact:
while (x < 1) {
x++;
}
x++ is pretty much equivalent to
some-temporary = x;
x = x + 1;
now use the value of some-temporary
So x = x++; is equivalent to
some-temporary = x;
x = x + 1;
x = some-temporary;
so you should be able to see why the part that adds 1 has no effect.
That is beacause x gets incremented after being used. So the expanded sequence of operations taking place would be:
oldX = x
x++, that is, x = x + 1
x = oldX
oldX being a fictitious variable.
What happens is that x is always assigned oldX, which in our case is 0, resulting in an infinite loop. The x++ part is just ignored.
Solution: replace x = x++ with x++ or, if you really want, x = ++x.
x = x++ doesn't do what you want. Replace it with x++.
x++ is equivalent to x = x + 1, so your assignment doesn't make sense.
More information about this can be found in answers here:
Why does this go into an infinite loop?
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Is there a difference between x++ and ++x in java?
I am reading the official Java tutorial and I don't get the difference between postfix and prefix (++x vs x++). Could someone explain?
++x: increment x; the value of the overall expression is the value after the increment
x++: increment x; the value of the overall expression is the value before the increment
Consider these two sections:
int x = 0;
System.out.println(x++); // Prints 0
// x is now 1
int y = 0;
System.out.println(++y); // Prints 1
// y is now 1
I personally try to avoid using them as expressions within a larger statement - I prefer standalone code, like this:
int x = 0;
System.out.println(x); // Prints 0
x++;
// x is now 1
int y = 0;
y++;
System.out.println(y); // Prints 1
// y is now 1
Here I believe everyone would be able to work out what's printed and the final values of x and y without scratching their heads too much.
There are definitely times when it's useful to have pre/post-increment available within an expression, but think of readability first.
++x increments x and then returns the value
x++ returns the value of x and then increments the variable
For example:
int x = 0;
int A = ++x; // A = 1
int B = x++; // B = 1
int C = x; // C = 2
Well, you get enough answers, I'm going just to worry you... Both post- and pre-increment operators can confuse code, so sometimes it is better to use just x+1 then you and other people definitely know what is going on there. Some examples:
int x = 5;
x = ++x;
System.out.println( x ); // prints 6
x = x++;
System.out.println( x ); // prints 6!
x = ++x + x++;
System.out.println( x ); // prints 14!
two last incrementing can be a source of problems to debug then (was watching that few times in my life...). x = x++ - it is evaluated before incrementing... So be careful!
++x is pre-incrementing and x++ is post-incrementing. With post-incrementing the value is increased after evaluation and with pre-incrementing the value is increased before evaluation.
Well, standing alone it's the same. but, if there are other operands involved - ++x will advance x and then apply the other operands, and x++ will first use x and then advance it.
Basically, ++x adds 1 to x before x is evaluated, while x++ adds 1 afterwards. It makes sense if you use it as an argument.
Let's start with x
int x = 3;
If we call System.out.println on x using the infix operator:
System.out.println(++x);
x will first increase to 4 and the println will output, "4". It is pretty much the same if we did:
x+=1;
System.out.println(x);
Let's imagine x equals 3 again. If we call System.out.println on x using the postfix operator:
System.out.println(x++);
It will first output the current value of x, "3", and then increase x. So it's like:
System.out.println(x);
x+=1;
Hope that helps.