• Post Reply Bookmark Topic Watch Topic
  • New Topic

for statement creates an infinite loop  RSS feed

 
Urs Waefler
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the code: The code contains an infinite loop and does not terminate. I thought it updates the variable i and when it reaches the value 10 the loop stops. Obviously that is not true. How does it work in detail?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12562
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This has nothing to do with the for-loop, and everythign to do with the post-increment operator. Check out this.
 
Urs Waefler
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me try to understand this code: i has the value 0. 0 is assigned to i, thus i has the value 0. After i is increased to 1. At this point I am not sure what happens. I think the assignment happend already when i gets 1 and the assignment happens only once. Is that correct?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nope. Post increment means "take the value of the variable, use it as the expression value, then increment it". After that, the assignment takes place.

So what value gets set into i by the assignment?
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure I understand your explanation. The link provided by fred clearly explains what happens.

i++ is evaluated as follows: the value of i is stored and then i is incremented by 1. The stored (pre-increment) value of i is then assigned to i. So i is incremented by 1 but then immediately overwritten by the pre-increment value.

 
Urs Waefler
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My understanding was correct, finally i has the value 0. First it takes the value 0, it assigns the value 0 to i, then the post increment happens. i gets 1, but this value is not assigned to i. i remains 0.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No you are wrong. The assignment happens after the post-increment not before it.  But the value that is assigned is the value before the post-increment.
 
Urs Waefler
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right, the assignment happens after the post-increment. But why does it assing the value before the post-increment?
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because it's a post increment ie use my current value and then increment me.

When you are making an assignment everything to the right of the equals is evaluated and then the assignment is made, hence the value of i is stored, then i is incremented by 1 and finally the pre-increment value of i is assigned to i.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12562
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Step 1) evaluate i (it's 0), and remember this in a temporary location.
Step 2) increment i.  i is now 1.
Step 3) assign that value we stored earlier to i. That value was 0, so set i to 0.
 
Paul Clapham
Sheriff
Posts: 22818
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Urs Waefler wrote:You are right, the assignment happens after the post-increment. But why does it assing the value before the post-increment?


Because the rules say to do that.

My rule is this:

Don't ever assign the result of an incrementing operator, whether pre- or post-, to another variable. And don't use the result in a calculation either.


That way I don't have to tax my brain by remembering annoying and unnecessary rules like this one. However if you're doing one of those certification exams then you're going to have to learn the rule.
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:My rule is this:
Don't ever assign the result of an incrementing operator, whether pre- or post-, to another variable. And don't use the result in a calculation either.

That way I don't have to tax my brain by remembering annoying and unnecessary rules like this one.

That's a good rule; made even better by the reason for following it.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!