Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Beginning Java and the fly likes A question about Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "A question about "Increment"" Watch "A question about "Increment"" New topic

A question about "Increment"

Jack Horner

Joined: Feb 22, 2005
Posts: 6
hello, everyone!
I think the result is 11. But it is 10. Why???
Andy Rayner
Ranch Hand

Joined: Dec 09, 2004
Posts: 36
dont have i=i++
thats like saying i = i = i +1

just have i++ this means i = i+1
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
"i++" is an expression with a side effect. Though its side effect is to increase the value of i by one, its *value* is the value of i before the increment. And that is the value you assign to i (using "=") *after* the increment happened.

With other words: the increment does *not* happen after the whole assignment, but only after the subexpression "i++" got evaluated (to the value of i), and *before* the assignment.

Does that help?

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Prashanth Lingala
Ranch Hand

Joined: Nov 13, 2004
Posts: 66
let us begin with a variable i whose value we have as 5.

Now Consider this...

You guessed it right...

Prashanth Lingala

Have A Nice Day !!!
I agree. Here's the link:
subject: A question about "Increment"
It's not a secret anymore!