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?
Related
for(i = 0; i<=timePassed; i++) {
y = r * x * (1 - x)
System.out.println(y)
So how do I reset the value of y every time the for loop loops so that i can assign a new value to it? I cannot wrap my head around it I have already tried to set y to 0 at the end of the loop right after printing y but that didnt seem to work. I also tried setting it to null but that gave me an error(?) I cannot understand why this is happening can someone please explain how to do this and what I did wrong?
Are you able to update your code block to include ALL code from your class.java file? It would help us see what could be wrong, because from this code snippet, y is not saved outside of the loop, therefore for every iteration (cycle) of the loop it would be reset and re-assigned.
As someone else has said, the y value does not change at any time in the loop, as all variables used (other than y) are declared outside of the loop. This loop simply prints the same y value "timePassed" amount of times.
Here's the code that you posted, including line numbers:
1. for(i = 0; i<=timePassed; i++) {
2. y = r * x * (1 - x)
3. System.out.println(y)
4.
Here are various comments + fixes:
Line 1: assume i is declared somewhere, like this: int i
Line 1: assume timePassed is declared somewhere and has a value set, like: int timePassed = 5;
Line 2: assume y is declared somewhere, like: int y;
Line 2: assume r and x are both declared somewhere, and have values set, like: int r = 1, x = 2;
Lines 2-3: need semicolons to terminate each line inside the loop: add ; at the end of each line
Line 4: add closing brace } to the "for" loop
Here is the code with those edits applied:
int timePassed = 5, y, r = 1, x = 2;
for (int i = 0; i <= timePassed; i++) {
y = r * x * (1 - x);
System.out.println(y);
}
Running that code produces this output:
-2
-2
-2
-2
-2
-2
The output doesn't change. Why? Because the calculation to determine each new value of "y" is the same each time: r * x * (1 - x). Despite being inside a loop, and with i changing each iteration through the loop, i is not used in the calculation at all.
Here's an edit to the code, removing the entire "for" loop (and i), and it produces the same -2 value:
int y, r = 1, x = 2;
y = r * x * (1 - x);
System.out.println(y);
-2
To demonstrate that y is being set each time through the loop, here's a variation of the code (including output) where i is included in the calculation for y.
int timePassed = 5, y, r = 1, x = 2;
for (int i = 0; i <= timePassed; i++) {
y = r * x * (1 - x) * i;
System.out.println(y);
}
0
-2
-4
-6
-8
-10
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.
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:
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.
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.