• 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

for loop with unary prefix on count

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This code prints
1
2
3
4
5

I'm curious as to why , I would have thought it would start from 2 , as the i gets incremented before the loop with a unary prefix ( or at least thats what I thought it would do).
 
Ranch Foreman
Posts: 408
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The for loop executes the initialization section (the first part in the big parenthesized expression) exactly once before the first execution of the loop.

The test, which is the part in there between the first ; and the 2nd ; gets executed every time we consider going thru the loop, and we enter the loop if the condition there is true.

The update section, after the last ; and before the closing ) gets executed each time thru the loop as if it was a statement at the bottom of the loop.

So the code is equivalent to this (but please don't write it that way except once to prove to yourself it is the same):


Now, if that last line said instead and we removed the first println, you would see what you expect, or if both println's were there, "i=1, 2, i=2, 3, i=3, 4), etc.
However, the value after the line completes execution is the same in either case of ++i or i++

So ++i and i++ have the same net effect in the code you wrote.  Some programmers like myself, would argue that ++i is more appropriate, but writing it as i++ does the same thing in terms of controlling the loop.  It only makes a difference when the value is directly used in a computation or written out somewhere, in which case ++i means do the increment first before using it, i++ means use the old value for the computation or side effect, and then increment it afterwards.  The variable winds up with the same value after we are done in either case.

You get interesting (or possibly surprising behavior) when you have multiple references to a variable that is getting pre- or post-incremented in one expression being evaluated.

You will see problems like that on mock and real certification exams.
 
Luke Moloney
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Jesse, thats a really great explanation and makes it much clearer.

Now I understand it conceptually. I could see what was happening before , but didn't get why it worked like that.

Thanks again
 
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