• Post Reply Bookmark Topic Watch Topic
  • New Topic

Increment question

 
jon ninpoja
Ranch Hand
Posts: 254
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
Sheriff
Posts: 22619
111
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: 12373
40
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).

 
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!