Win a copy of Pro Spring MVC with WebFlux: Web Development in Spring Framework 5 and Spring Boot 2 this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
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: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



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
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic