Java Code below :
int x = 10;
while (x != 15){
x = x++;
System.out.println("Value X " + x);
}
execute : endless Loop ? why ?
Java Code below :
int x = 10;
while (x != 15){
x = x++;
System.out.println("Value X " + x);
}
execute : endless Loop ? why ?
I may be wrong but x++
incrementes x after reading it so x = x++
means x = x
.
You should use x = ++x
instead.
See this question
Becouse you assign the old value of x
to x
befor you increase the value!
Try this:
int x = 10;
while (x != 15){
x++;
}
or
int x = 10;
while (x != 15){
x = ++x;
}
x++; increments the value of x then returns the old value of x.
THEN x = (that number) executes, meaning the mutation x++ did of x is undone.
Because x = x++
will assign the result of the operation x++
to x
. What you want is only x++;
, which increments the value of x
by one.