incrementation of variable
posted 20 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
hi friends;
i have a doubt abt incrementation of variable.
see the following code:
In the above code the o/p is 1.
But by logical view the o/p should be 2 as shown in the commented code.
Why x is not getting incremented after line 3?
Bye......
shubha.
( tags added)
[ June 16, 2005: Message edited by: Barry Gaunt ]
i have a doubt abt incrementation of variable.
see the following code:
In the above code the o/p is 1.
But by logical view the o/p should be 2 as shown in the commented code.
Why x is not getting incremented after line 3?
Bye......
shubha.
( tags added)
[ June 16, 2005: Message edited by: Barry Gaunt ]
Shubhada
posted 20 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Hi,
Because x = x++; is still the same.
Your saying x = x++; it's not being incremented x is just = x.
In x++ the ++ (postfix is being ignored) you need to use ++x; if you want to increment it.
Hope this made sense!
Because x = x++; is still the same.
Your saying x = x++; it's not being incremented x is just = x.
In x++ the ++ (postfix is being ignored) you need to use ++x; if you want to increment it.
Hope this made sense!
posted 20 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
You may find that Corey's tip help to explain what's happening.
Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
posted 20 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
your commented code is different than the code you run - they are not equivilent.
here's why...
JUST before we get to line three, x is 1. we now start doing what line 3 says to do...
first, get x. that's 1. we'll remember that value for later, first i have to take care of that ++. So, now i increment x. x is 1, so i'll increment it to 2. now, lets take care of that assignment thing. the right hand side of my statement was... let's see, i remembered that... oh yeah!!! it's 1!!!.
so now, i'll assign 1 to x.
and so it prints 1.
here's why...
JUST before we get to line three, x is 1. we now start doing what line 3 says to do...
first, get x. that's 1. we'll remember that value for later, first i have to take care of that ++. So, now i increment x. x is 1, so i'll increment it to 2. now, lets take care of that assignment thing. the right hand side of my statement was... let's see, i remembered that... oh yeah!!! it's 1!!!.
so now, i'll assign 1 to x.
and so it prints 1.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Shubhada Nandarshi
Ranch Hand
Posts: 59
posted 20 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Hi everyone;
i have still doubt,
if we run the code as;
class Test{
public static void main(String args[]) {
int y=0;
++y;
y++;
System.out.println(y);
}
}
gives me o/p as 2.In the 1st code I did the same operation.
i have still doubt,
if we run the code as;
class Test{
public static void main(String args[]) {
int y=0;
++y;
y++;
System.out.println(y);
}
}
gives me o/p as 2.In the 1st code I did the same operation.
Shubhada
Shubhada Nandarshi
Ranch Hand
Posts: 59
posted 20 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Hi everyone;
i have still doubt,
if we run the code as;
class Test{
public static void main(String args[]) {
int y=0;
++y;
y++;
System.out.println(y);
}
}
gives me o/p as 2.In the 1st code I did the same operation.
i have still doubt,
if we run the code as;
class Test{
public static void main(String args[]) {
int y=0;
++y;
y++;
System.out.println(y);
}
}
gives me o/p as 2.In the 1st code I did the same operation.
Shubhada
posted 20 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
x++;
and
x = x++;
Do NOT do the same thing.
The first one says "increment x."
The second one is a bit confusing. It says...
- grab the current value of x (let's assume it's 0)
- increment x (it's 1)
- assign the grabbed value to the variable on the left (x)
- so x gets 0 (the value we grabbed)
If you changed:
x = x++;
to:
x = ++x;
You'd get something more like what you expect because x gets incremented BEFORE the assignment takes place.
Of course, no one in their right mind would actually write either of these since just writing:
x++;
is MUCH more clear.
Hope that helps.
[ June 16, 2005: Message edited by: Dave Wood ]
and
x = x++;
Do NOT do the same thing.
The first one says "increment x."
The second one is a bit confusing. It says...
- grab the current value of x (let's assume it's 0)
- increment x (it's 1)
- assign the grabbed value to the variable on the left (x)
- so x gets 0 (the value we grabbed)
If you changed:
x = x++;
to:
x = ++x;
You'd get something more like what you expect because x gets incremented BEFORE the assignment takes place.
Of course, no one in their right mind would actually write either of these since just writing:
x++;
is MUCH more clear.
Hope that helps.
[ June 16, 2005: Message edited by: Dave Wood ]
Co-Author of <a href="http://www.oreilly.com/catalog/jswing2" target="_blank" rel="nofollow">Java Swing</a><br />Co-Creator of <a href="http://www.sun.com/training/catalog/courses/CX-310-055.xml" target="_blank" rel="nofollow">SCJP 5.0</a> and <a href="http://www.sun.com/training/certification/java/associate_beta.xml" target="_blank" rel="nofollow">SCJA</a> exams
| You can't have everything. Where would you put it? Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing |








