# simple int

PUNNU GULATI
Greenhorn
Posts: 6
hello everyone
int i;
i=i++;
s.o.p(i);
result is 0
why

Lalit Sha
Greenhorn
Posts: 28
Punnu,
Could you please write the code completely.
Lalit.

Scott Pedigo
Greenhorn
Posts: 15

[This message has been edited by Scott Pedigo (edited February 19, 2001).]

Grant Crofton
Ranch Hand
Posts: 154
Like Scott says, when you do

i gets incremanted after the assignment, so x==0;
if you did

i would be incremanted before the assignment, so x==1.
You wouldn't normally do i=i++ - perhap's you're thinking of i++ or i+=1 or i=i+1, which all simply increment i by 1.

Frank Carver
Sheriff
Posts: 6920
As an aside, I feel strongly that pre-increment (++i) works in a much more intutive way than post-increment (i++), and blame lots of textbook and programming standards writers (starting with Kernighan and Richie) for using post-increment so much where it is not needed.
Personally, I always use pre-increment unless I have a real need for the special behaviour of post-increment, and I try and teach my development teams the same habit.

sona gold
Ranch Hand
Posts: 234
when u say
int i;
i = i++;
System.out.println(i)
The output is 0 because
in
int i ;
i is set to 0
then when u say
i = i + +
this means increment the value of i after performing the task assigned
in this case the task is System.out.println(i)
hence o
if u put it in a loop
the next time
i = i ++ is executed
it will print 1

Grant Crofton
Ranch Hand
Posts: 154
Yeah, I was always taught post-increment, and now I always use it unless I have a reason not to. I suppose pre-increment is more intuative, but you tend to get into a 'style', don't you.
I think maybe people teach i++ because it's more like i=i+1. Or maybe it just rolls of the tongue easier.

Frank Carver
Sheriff
Posts: 6920
Depends how you say it. I find "increment i" to be more sensible to say that "i increment" (which usually begs the question "you increment what?")

Cindy Glass
"The Hood"
Sheriff
Posts: 8521
Java humor

Grant Crofton
Ranch Hand
Posts: 154
i plus plus.