for
statement provides a compact way to iterate over a range of values. Programmers often
refer to it as the "for loop" because of the way in which it repeatedly loops until a particular
condition is satisfied. The general form of the for
statement can be expressed as follows:
for (initialization; termination; increment) { statement(s) }
for
statement, keep in mind that:
false
, the loop terminates.
The following program,
ForDemo
, uses the general form of the for
statement to
print the numbers 1 through 10 to standard output:
class ForDemo { public static void main(String[] args){ for(int i=1; i<11; i++){ System.out.println("Count is: " + i); } } }
Count is: 1 Count is: 2 Count is: 3 Count is: 4 Count is: 5 Count is: 6 Count is: 7 Count is: 8 Count is: 9 Count is: 10
for
statement, so it can be
used in the termination and increment expressions as well. If the variable that controls
a for
statement is not needed outside of the loop, it's best to declare the
variable in the initialization expression. The names i
, j
, and k
are often used to control for
loops; declaring them within the
initialization expression limits their life span and reduces errors.
The three expressions of the for
loop are optional; an infinite
loop can be created as follows:
for ( ; ; ) { // infinite loop // your code goes here }
The for
statement also has another form designed for iteration through
Collections
and
arrays
This form is sometimes referred to as the enhanced for statement, and can be used to make
your loops more compact and easy to read. To demonstrate, consider the following array, which holds the numbers 1 through 10:
int[] numbers = {1,2,3,4,5,6,7,8,9,10};
The following program,
EnhancedForDemo
, uses the enhanced for
to loop through the array:
class EnhancedForDemo { public static void main(String[] args){ int[] numbers = {1,2,3,4,5,6,7,8,9,10}; for (int item : numbers) { System.out.println("Count is: " + item); } } }
item
holds the current value from the
numbers array. The output from this program is the same as before:
Count is: 1 Count is: 2 Count is: 3 Count is: 4 Count is: 5 Count is: 6 Count is: 7 Count is: 8 Count is: 9 Count is: 10
for
statement instead of
the general form whenever possible.