For Loops

When to use: For loops are used when (i) we need to loop a specific number of times or (ii) we want to step a variable through a series of values.
It's hard to be more specific than this, as there are so many different situations when for loops are used. (You'll get a feel of where to use them by doing the excercises with for-loops listed on the GoogleDocs spreadsheet. ??) The only other type of loop is the WHILE loop - so you'll have to use one or the other.


In some ways, for loops have a syntax that parallels that of IF statements.

if (condition) { statements }

for (.....) { statements }

As in all programming languages, there are three parts to the for loop:

The simplest loop is written like this (you can see all of the 3 parts mentioned above)
for (int i=0; i<=10; i++) { ... }

The loop will end as soon as the middle expression (i <=10) is false.

EXAMPLE: This look will execute 10 times and print out the value of i each time:

for (int i=0; i<10; i++) {
i will go from 0 to 9 (that's ten numbers)

You can write an infinite loop like this: for ( ; ; ) { ... }

Loop Counters

For loops (almost) always have a loop counter — a variable that is used to control when the loop stops.

If you only use this variable inside the loop, then it is declared as part of the for loop and n disappears once the loop exits.

for (int n=0; n<=10; n++) { ... }

If you need to use the value of n outside the loop, then do something like the following:
int n; for (n=0; n<myArray.length; n++) { if (myArray[n] == 100) break; } System.out.println("100 found at location " + n);

Never change the loop counter inside the loop. If you do this, then your program can easily become unpredictable and extremely hard to debug.
So in the previous example, n is only ever changed in the for loop increment section, ie. n++.   n is never altered by the code inside the loop.

Break, Continue, and other ways of exiting loops

break; is extremely useful. It is used to break out of a loop at a certain point. See the example above.
NOTE that BREAK will only break out of the inner most loop if you have nested loops.

It is possible to break out of a loop by manipulating the loop counter. In the example above, the same thing could have been accomplished by setting n=1000;
This is considered poor programming style, since if the loop condition changes, it will not longer exit properly.

If the loop is in a function other than main, one could use return; to exit the function. This exits the loop AND the function. This may not be what you want to do if there is other stuff still to do in the function

System.exit(0); is not really a method of breaking out of a loop. It terminates the program completely and immediately. Not a good way of programming!

continue; does NOT break out of a loop. It skips the rest of the statements in the loop and begins the next iteration.
for (int i=0; i<=100; i++) { ... //stuff if (ball.x < 0) continue; ... //more stuff } This means that if ball.x is less than zero, all of the statements called "more stuff" will be skipped, the loop counter will be incremented (i++), and the loop will continue with the next iteration.

All of these will also work with while loops.

For-each loops

These will be discussed with ARRAYS