This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Five Lines of Code and have Christian Clausen on-line!
See this thread for details.
Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Update clause in loop statement using pre- or post-increment: does it make a difference?

 
Ranch Hand
Posts: 113
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Consider the following question, coming from one of Enthuware's mock exams:

What will be the output if you run the following program?

The answer being: 0 0 will be printed followed by 1 1.

I expected the loop not to run at all, and that simply 1 1 would be printed. j has a pre-increment operator (++j), so my assumption was that j will be incremented before the loop checks whether the code within curly brackets should be executed.

However, it appears now that the following is actually happening:
1) j is set to 0.
2) it is checked whether j is less than 1, which is true at this point. The compiler then decides that no matter what, the code within curly brackets will be executed.
3) j is then incremented, but it's too late: the code will be executed.

Is this order of steps correct?

Also, doesn't this mean it essentially doesn't matter whether the update clause of a loop uses pre- or post-increment? The results for i and j are the same, after all.

Thanks again!

Regards,
Shane
 
Bartender
Posts: 4001
156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Shane Jensen wrote:(...) Is this order of steps correct?


Almost.
At first, i = j = 0. so the code in the curly braces is executed. Then j and i are incremented. Since then j >= 1, the curly code is not executed anymore.
That i j = 1 1 is still being printed, is caused by line 8.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Shane Jensen wrote:Is this order of steps correct?


No, the correct execution flow can be found here.

Shane Jensen wrote:The compiler then decides that no matter what, the code within curly brackets will be executed.


The compiler does not execute any code at all!

All the following for loops will produce the same output
 
Rebecca Wolf
Ranch Hand
Posts: 113
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, guys.

I also found the following topic, which discusses the same question (for any future readers): https://coderanch.com/t/516989/certification/point-pre-increment-operation-loop Should have used that search functionality ;)
 
Rebecca Wolf
Ranch Hand
Posts: 113
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Roel De Nijs wrote:

Shane Jensen wrote:Is this order of steps correct?


No, the correct execution flow can be found here.


How is the flow described on that page any different from the steps I mentioned?
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Shane Jensen wrote:How is the flow described on that page any different from the steps I mentioned?


Apologies! There's indeed no difference. I probably have misread (or misinterpreted) something in the steps you've described.
 
Rebecca Wolf
Ranch Hand
Posts: 113
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Roel De Nijs wrote:

Shane Jensen wrote:How is the flow described on that page any different from the steps I mentioned?


Apologies! There's indeed no difference. I probably have misread (or misinterpreted) something in the steps you've described.


No problem.
 
    Bookmark Topic Watch Topic
  • New Topic