Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

simple int

 
PUNNU GULATI
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello everyone
int i;
i=i++;
s.o.p(i);
result is 0
why
 
Lalit Sha
Greenhorn
Posts: 28
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Punnu,
Could you please write the code completely.
Lalit.
 
Scott Pedigo
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


[This message has been edited by Scott Pedigo (edited February 19, 2001).]
 
Grant Crofton
Ranch Hand
Posts: 154
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java humor
 
Grant Crofton
Ranch Hand
Posts: 154
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i plus plus.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic