for ( statement1; expression2;
statement3 )
{
statement4;
}
The for
loop is typically set up as follows.
statement1
initializes the loop variable
expression2
is a boolean
expression
statement3
alters the key value, usually via an increment/decrement statement
statement4
is the task to be done during each iteration
Notice that after the statement is executed, control passes to the increment/decrement statement, and then back to the Boolean condition.
A for
loop is appropriate when the initialization value and number of iterations is known in advance. The above example of printing 10 numbers is best solved with a for
loop because the number of iterations of the loop is well defined.
-
Constructing a for
loop is easier than a while
loop because the key structural parts of a loop (initialization, loop boundary, and increment/decrement statement) are contained in one line. It is also easier to visually check the correctness of a for
loop because it is so compact.
-
A while loop is more appropriate when the boundary condition is tied to some input or changing value inside of the loop.
- Here is an interesting application of a for loop to print the alphabet:
char letter;
for ( letter = 'A'; letter <= 'Z';
letter++ )
{
System.out.print(letter);
}
The increment statement letter++ will add one to the ASCII value of letter.
- A simple, but time-consuming error to find and fix is the accidental use of a null statement.
for ( loop = 1; loop <= 10; loop++ );
// note ';'
System.out.print(loop);
The semicolon placed at the end of the first line causes the for
loop to do "nothing" 10 times. The output statement will only happen once after the for
loop has done the null statement 10 times. The null statement can be used as a valid statement in control structures. Make sure you pay attention to your enclosing {}.
- There are two basic options for the variable used in the
for
loop. The variable can either be declared beforehand and therefore initially have a value set at another point in the program, or it can be declared and initialized within the for
loop itself. Consider the following two for
loops:
int number = 1;
for( ; number <= 10; number++ )
{
System.out.println(number);
}
for( int a = 1; a <= 10; a++ )
{
System.out.println(a);
}
Notice how the first statement of the first for
loop is blank. This is because the int
variable number has already been declared and initialized. Blank statements within the for
loop are allowed. Now, both for
loops appear to do the exact same thing, printing out all the numbers from 1 to 10. However, there are several key differences. In the first example, number
may be changed to any number desired during the run of the program. Instead of setting it to one, a programmer could set it to the value of a function such as int number = getValidNumber()
. This gives additional power to the programmer. Another difference between the two loops is the scope of the variables number
and a
. Because number
was declared outside of the for
loop, the value of number
may be used after the for
loop. However, a
was declared within the for
loop and thus will not be usable past the end bracket of the for
loop.