• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Question on Flowcontrol?

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Test
{
public static void main(String[] args)
{
int i = 0, j = 5;
lab1 : for( ; ; i++)
{
System.out.println(i);
for( ; ; --j)
if( i >j )
break lab1;
}
System.out.println(" i ="+i+" , j = "+j);
}
}
//The answer is: i=0, j= -1
I cannot understand why "i" is 0 instead of 1, since it past the for(;;i++) loop.
Thank you for your explaination.

[This message has been edited by Ivey Zhang (edited February 14, 2001).]
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ivy
the third increment operator of for loop (i++ )will be executed after the loop completes and begins the next iteration.
for example this is what's happening in the for loop :-
1. initialization
2. comparison
3. block execution
4. increment -> 2
At 3 you're breaking from the loop Hence x is not incremented.
Hope I've conveyed the message properly!
 
Ivey Zhang
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pravin,
I see, I see
Thanks a lot!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic