• Post Reply Bookmark Topic Watch Topic
  • New Topic

Increment question

 
jon ninpoja
Ranch Hand
Posts: 216
2
  • 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
Marshal
Posts: 22365
102
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: 12297
36
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!