for loop : Java Glossary

for loop

The for is Java’s multipurpose loop controller. It is most commonly used in this form:

for ( int i=0; i<n; i++ )
   {
   System.out.println(i);
   }

You can leave out any of the three sections of the

for ( init; should-I-continue-looping?; increment )

sections. If you leave out the should-I-continue-looping? boolean expression, it is assumed true. This means you can use a for to simulate while and do whileloops.

The other common use of for is to iterate over a collection like this:

for ( Iterator aIter = myCollection.iterator(); aIter.hasNext(); )
   {
   Thing item = (Thing) aIter.next();
   // do something with item
   }

Note the empty increment portion of the for. You have to do the next inside the loop body for iterators. for is preferable to using a while loop because the aIter variable is automatically made local to the loop.

For loops can have a dual index, like this:

for ( int i=0, j=n; i<n; i++,j-- )
   {
   /* loop body */
   }

or like this:

{
   int i,j;
   for ( i=0,j=n; i<n; i++,j-- )
      {
      /* loop body */
      }
}
or
{
   int i;
   float f;
   for ( i=0,f=n; i<n; i++,f*=.5 )
      {
      /* loop body */
      }
}

But, due to a hopelessly muddled for syntax dating back to C, (deriving from the attempt to reuse comma and semicolon in incompatible ways) you cannot write code like this:

for ( int i=0, float f=n; i<n; i++,f*=.5 )
   {
   /* loop body */
   }
For loops allow two kinds of controlled goto, the break and the continue. break leaps out of the loop entirely. continue jumps to the end of the loop and continues looping. By labelling your for loops with words like inner: and outer: you can break out of two or more levels of nesting with break outer; or continue outer;. break and continue work with while, do while and switch statements as well.

CMP homejump to top

available on the web at:

http://mindprod.com/jgloss/forloop.html
  

optional Replicator mirror
of mindprod.com
on local hard disk J:

J:\mindprod\jgloss\forloop.html
logo
Please email your , letters to the editor, errors, omissions, typos, formatting errors, ambiguities, unclear wording, broken/redirected link reports, suggestions to improve this page or comments to Roedy Green : feedback email. If you want your message, your name or email kept confidential, not considered for public posting, please explicitly specify that. Unless you state otherwise, I will treat your message as a letter to the editor that I may or may not publish in the feedback section. After that, it will be too late to retract it. If you disagree with something I said, especially when sending an ad-hominem attack, a rant composed mainly of obscenities or a death threat, please quote the offending passage and cite the web page where you found it, tell me why you think it is wrong, and, if possible, provide some supporting evidence. I can’t very well fix erroneous or ambiguous text if I can’t find it.
Blog
IP:[65.110.21.43]
Your face IP:[50.19.155.235]
You are visitor number 228,044.