programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Ron McLeod
• Paul Clapham
• Jeanne Boyarsky
• Liutauras Vilda
Sheriffs:
• Rob Spoor
• Bear Bibeault
• Tim Cooke
Saloon Keepers:
• Tim Moores
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Piet Souris
Bartenders:
• Frits Walraven
• Himai Minh

# Chapter 2, review question #9

Greenhorn
Posts: 2
• Number of slices to send:
Optional 'thank-you' note:

I know that the code above outputs an infinite loop, but I don't understand why. I assumed

would simply update the value of i like in a regular for loop. Why doesn't that happen?

Greenhorn
Posts: 3
• 1
• Number of slices to send:
Optional 'thank-you' note:
consider this

This happens because of operator precedence. PPOMASRELSTA - pg 52

steps are:
1. i is a primitive assigned the value of 0
2. i is incremented with post unary operator i++. This FIRST returns i and then increments it. So what has been returned is 0 and THEN i is set to 1
3. the LOWER precedence assignment operator = is applied and sets i to the value returned. That value is 0

so i, in the middle of the operation, is set to 1  and then immediately set back to 0.

Greenhorn
Posts: 9
• Number of slices to send:
Optional 'thank-you' note:
Hi,
I have the same issue with this question.
If I type the following code:

I don't understand why i isn't 1.

Thank you.

Matteus White
Greenhorn
Posts: 3
• 1
• Number of slices to send:
Optional 'thank-you' note:
i++ returns 0
Then increments i to 1
The assigns i to the return value, of 0

So in the end, after the assessment using =, i is 0

 Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters?