This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Increment question  RSS feed

 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how come this:



gives values:
11 and 0

shouldnt i be printing still as 10? as isnt the calculation tied to n? does the calculation in n also change i's value?

 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jon ninpoja wrote:
shouldnt i be printing still as 10? as isnt the calculation tied to n? does the calculation in n also change i's value?


Well, the i variable started out as 10. It get incremented once. There are no other operation that affects it. So, why should it be anything else but 11?

Henry
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the evaluation goes something like this...

set i to 10.
get the value of i and remember it - it's 10.
increment it - i is now 11.
now take that values we remembered, and do the mod 5 of it, giving 0.
Assign 0 to n. 

print i (which is now 11)
print n (which is 0).

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!