java pre increment and post increment and self-assignment in loop [duplicate] - java

This question already has answers here:
What is x after "x = x++"?
(18 answers)
Java increment and assignment operator [duplicate]
(6 answers)
Closed 4 years ago.
I expected post-increment and pre-increment of a variable and assignment of the result to itself to work a bit differently. But while the latter works as expected, the former runs as infinite while loop. Could someone please point out what i am missing here?
int y = 0;
int z = 4;
while(y<z)
{
System.out.println(y);
y =y++;//this prints 0 infinite times, shouldn't why be assigned values 0,1,2,3 with each pass?
//y =++y;//this works as expected
}
Thank you

As described in this StackOverflow answer, post increment works by storing a copy of y, adding 1 and returning the copy. This means that the return value of y++ is not y+1, but still only y. Since you are overwriting y with y++, you are essentially just saying y = y.

Related

n = n--; why same value as before [duplicate]

This question already has answers here:
Difference between b=b++ and b++ [duplicate]
(4 answers)
What is x after "x = x++"?
(18 answers)
Closed 1 year ago.
The title is self-explanatory. Consider the following code:
int n = 5;
n = n--;
It gives n = 5.
As far as I understood the expression n-- is first evaluated, returning 5 (i.e. POSTdecrement). This expression gets assigned to the LHS, here n.
After this execution, n gets diminished. Thus I expected n = 4.
Why don't I see 4?
n-- yields 5 and then sets n to 4
n = sets n to the value of the right-hand expression, which is 5
Mixing the increment/decrement operators with an assignment to the same variable rarely does anything useful.
This is not fully correct, there is 2 forms to do it --n and n--, thats is where you will do the rest on N.
With the --n before you first rest and then return the value, and the other side is the opposite.
You first pass the value and then rest the value, try doing it the other way.
Given n=5, n-- return 5 but n value is 4.
Given n=5, --n return 4 and n value is 4.
That's why n has still the same vlaue

Post incrementing same variable not working [duplicate]

This question already has answers here:
What is x after "x = x++"?
(18 answers)
Closed 3 years ago.
I have a simple question that I cannot find the answer to in any java videos or forums. If someone can please explain it to me, why my simple post increment is not working. I start with a value of 2 and never see it increment after 1 or 30 lines.
MY CODE
int x=2;
x=x++;
System.out.println("value of x "+x);
System.out.println("value of x "+x);
System.out.println("value of x "+x);
all the answers are 2. What happened to my increment? x never got 1 added to it post the assigning.
this works if I use 2 different variables and x goes up but I need to see it with one/same variable
Remove the x=x++;:
int x=2;
x++;
System.out.println("value of x "+x);
System.out.println("value of x "+x);
System.out.println("value of x "+x);

how to set a variable to 0.001 in JAVA (android) [duplicate]

This question already has answers here:
Int division: Why is the result of 1/3 == 0?
(19 answers)
Closed 3 years ago.
when i want to get the result of 100/100000..
i got only 0.
Example
int one = 100;
int two = 100000;
int result = one/two;
toast(result); //Result is 0
Hey there "int" data type only stores integer values and not the decimals.
So if you divide 3 with 2 you would get 1 as answer instead of 1.5 .
Int just ignores the decimals .
You need to choose float or double data type for this to work.
Your variable named result must be declared and casted to float data type.
Appreciate the effort and mark this as answer if it helps you.....

Some java Fundamental [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Update::
1). problem ::
i wrote System.out.println("welcom"); &
System.out.println("india");
i want like :: welcome india
but give::
welcome
india
1.
println always prints a newline character. To get what you want, try:
System.out.print("welcome ");
System.out.print("india\n");
2.
x++ is post-increment, ++x is pre-increment.
So in:
int x = 5;
int y = ++x;
y will be 6.
But in:
int x = 5;
int y = x++;
y will be 5.
In 1: What is the difference between System.out.print and System.out.println ?
In 2: What does the ++ operator do and when does it do it?
println
stands for "print line" so, if you use println you have a "carriage return" and the next println will print a new line (and so on...)
x++ is different to ++x: the second increment variable's value and then goes on, the first goes on with statement and then increment variable's value.
System.out.println("welcom"); & System.out.println("india");
you wrote now if you want same line
System.out.print("welcom ");
System.out.print("india");
And
x++ is Post-increment
Post-increment : add 1 to the value.
The value is returned before the increment is made, e.g.
x = 1;
y = x++;
Then y will hold 1 and x will hold 2
and
++x is pre increment
Pre-increment : add 1 to the value.
The value is returned after the increment is made, e.g.
x = 1;
y = ++x;
Then y will hold 2 and x will hold 2.
1) To print a string without printing an endline use System.out.print()
2)
x++ - increment x, and return x's previous value (before increment)
++x - increment x, return x's current value (after increment)
These are the Post and Pre fix versions of incrementing.
println() prints a line and anything printed after the println statement (using any print statement) is printed in the next line.
Second thing is the difference between ++x and x++. ++x increases the value of x BEFORE doing anything else in the statement (pre-increment), and x++ increases the value of x AFTER it performs anything else in the statement (post increment).
For example:
x = 1;
y = ++x; // value of y is 2, value of x is 2
but
x = 1;
y = x++; // value of y is 1 and x is 2
try System.out.print & 1 happens before a condition/expression takes place; the other after;
System.out.println() has give next line as it complete statment & ++x are use for preincremet & C++ is post incremental.

Different result same expression in C, C++ and Java. WHY? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Could anyone explain these undefined behaviors (i = i++ + ++i , i = i++, etc…)
int i = 10;
int n = i++*5*i;
Output
value of n = 550 (in Java)
value of n = 500 (in C and C++ )
Why not same result? Why different?
In Java, this is a well defined operation. It will:
increment i (it's now 11);
Produce the old value of i (10), because you used the postfix increment operator;
Multiply that by 5 (10*5 = 50);
Multiply that by the current value of i (50*11 = 550);
In both C and C++ this operation has undefined behaviour, so anything could happen. If anything could happen, that explains the results, whatever they are, and whether they make sense to you or not.
In C and C++, operations such as:
j = i++ + i;
are undefined, due to lack of sequence points. In Java, they are well defined. Therefore, you could see a difference in results.
Because what you are doing is undefined. The increment operator should not be placed in expressions of assignment with the variable being incremented.
i = i++; //undefined
n = i++ + i; // also undefined

Categories

Resources