• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

The i++ operator

 
Arthur Blair
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I havea found a mock exam question that is puzzling me.



Why does this print out 1,0?

If i = i++; makes i equal to 0, I don't understand how a "1" is passed in as an arguement to m().
[ February 03, 2006: Message edited by: Arthur Blair ]
 
fred rosenberger
lowercase baba
Bartender
Posts: 12143
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think this is what happens...

i is 0.

we evaluate the first term (i++). since we have the post increment, we figure what i is, giving us 0 for the value of the term, then we increment i to 1.

we call m(i), where i is now 1.

m(i) prints 1, and returns a 0.

we now do the addition. we previously evaluated that first term to be 0, we add it to the 0 we got back from the function call, and get 0.

i is then assigned the value 0.
[ February 03, 2006: Message edited by: fred rosenberger ]
 
steven gerrard
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int i = 0;
i = i++ + m(i);
when u use i++ value used to evaluate expression is 0 however value of i becomes 1
this value is used in the function call m(i)
hence 1 is printed
function returns 0
hence new i = 0+0
hence the output of 1,0
 
Kenny Chen
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In short, is that what it means?

i = i++;

i always equals to the original value, the increment discards?
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeap.
 
shan Iyer
Ranch Hand
Posts: 391
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please explain the following:

int i=0;
i=i++;
System.out.println(i);

o/p is "0".

int i=0;
int k=i++;
System.out.println(k);

o/p is "1".

If o/p is "0" in first case, second case also it should be 0, but its 1. WHy??
 
Sonali Salunkhe
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Ham {
static int m(int i)
{
System.out.print(i + ",");
return 0;
}
public static void main (String[] args)
{
int i = 0;
i = i++ + m(i);
System.out.print(i);
}
}


This happens :

i=i++ + m(i);

i= 0 + m(1); // since its post increment , its value is 0 for first time, then it incerement by 1, this new value is send as argument to m

i= 0+ 0;
i=0;

So it prints: 1,0
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shankar,


o/p is "1".


Could you confirm that the output is "1".
It is supposed to output "0".
[ February 06, 2006: Message edited by: Satou kurinosuke ]
 
Tilo Hemp
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just to avoid confusion for anybody reading this: the output of

int i=0;
int k=i++;
System.out.println(k);

is 0 (at least on my system )
 
Arthur Blair
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies guys, I think I am close to deciphering this one.

int k=0;
k= k++ + k++;
System.out.println("k:"+k);

So why does this print out 1? Following what has been said, I would have thought:

int k = 0;
k = k++ + k++;
k = 0 + 0;
k = 0;

But this doesn't happen. It in fact evaluates to 1.
...so it looks as if one of the k increments gets evaluated and one gets disregarded.

Similarly:

int k = 0;
k = k++ + k++ + k++;
I would have thought that this means:
k = 0 + 0 + 0;
k = 0;

... but in this example k becomes 3.

What I think is happening is this:
int k = 0;
k = k++ [this k evaluates to 0, but increments the next k]
+ k++ [this k evaluates to 1, and increments the next k]
+ k++; [this k evaluates to 2, but the increment is DISREGARDED because there is no further use of k in the line of code]

So we have: k = 0 + 1 + 2 = 3;

Similarly:

int k = 0;
k = k++ + k++ + k++ + k++;
evaluates to:
k = 0 + 1 + 2 + 3;
And thus:
k = 6;

I'm just making assumptions here. I don't understand the reason why the last increment gets disregarded. So if someone has any more stable knowledge, please share it!
 
fred rosenberger
lowercase baba
Bartender
Posts: 12143
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


for the same reason.

k is 0.

then, since we have k++, we figure out what k is (0), then increment it.

we now hit the second k++. what is k? it's now 1. remember that, and increment k. k is now two.

do the addition - 0 + 1 = 1

assign that to k.

print k, which is now 1.

the lesson to learn from all this is NEVER write code like

k=k++;
[ February 06, 2006: Message edited by: fred rosenberger ]
 
steven gerrard
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
last k is not disregarded

evaluate following :
int k = 0,i=0;
i = k++ + k++ + k++ + k++;
System.out.println(k); //will print 4

see wat happens is that after the last increment k becomes 4
after that ur expression :k= k++ + k++ + k++ + k++; is evaluated

so initially k is 0 then 1 then 2 then 3 then 4 and then 0+1+2+3
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The kind of things giving us headaches, but that we'll never use in real life.
Wouldn't that be called a waste of time ?

Well, I've personnaly never seen things like i = k++ + k++ + k++ + k++;
And I'd kill my coworker if he dares trying such a thing
 
Devi Sri
Ranch Hand
Posts: 115
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int k=0;
k= k++ + k++;
System.out.println("k:"+k);
----------------------------------------------------------
The execution goes on like this:

int k=0;
k = k++ + k++;
k = 0 + k++; // after this k becomes 1
k = 0 + 1; // after this k becomes 2
k=1; // but here addition and assignment operations give, k=1.

------------------------------------------------------------
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic