Win a copy of Java Challengers this week in the Java in General 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
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

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!
 
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