• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

for loop-Q

 
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class LoopTest100{
static int i,j;
public static void main(String[] args) {
for (i = 0;i <3;i++,System.out.print(i)) { }
System.out.println();
for (j = 0;j<3;++j) {
System.out.print(j);
}
}
}

//Ouput:
//1,2,3
//0,1,2

some one please explain the difference.
 
Ranch Hand
Posts: 1880
Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This will clear your doubts.
http://forum.java.sun.com/thread.jspa?threadID=577684&messageID=2900049
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That thread dealt with "int x=1,y; y=x++ + ++x + ++x;", an interesting case but not the question at hand.

The trick here is that the first print() is in the increment part of the for statement, an unusual but legal structure. Think about the value of i after i++ during each iteration.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another hint: think how you could rewrite the first for loop into an equivalent for loop.
[ December 31, 2004: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a difference indeed. The point is that the increment expression is evaluated at the end of the loop. That means that in the first loop the expression i++ will be evaluated to 1 in the first loop, then will be printed by the System.out.print(i). So the printing will start in 1 not in 0.

The second loop is different because the code block of loop is evaluated before the increment expression, so the value for j is evaluated before it is incremented. This is why this loops starts with 0.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As I mentioned previously in my hint, the code: can be rewritten:


It is now clear that 123 will be printed.
 
You get good luck from rubbing the belly of a tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic