Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A question about "Increment"

 
Jack Horner
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello, everyone!
I think the result is 11. But it is 10. Why???
thanks
 
Andy Rayner
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmm
dont have i=i++
thats like saying i = i = i +1

just have i++ this means i = i+1
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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?
 
Prashanth Lingala
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
let us begin with a variable i whose value we have as 5.


Now Consider this...



You guessed it right...

Regards
Prashanth Lingala
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic