Another powerful tool in programming is the loop, which provides
for repetitive execution of a section of code. In Java there are
three such loop statements:
for
Statement
The for
loop provides a neat and readable way to execute a loop with initialization,
an update action for every repetition step, and a test for ending
the loop all in one line:
for(initialize
; boolean test ; step update ) statement ;
or
for(initialize
; boolean test ; step update )
{
statement 1;
statement 2;
...
}
where
- initialize
represents whatever initial setup
actions you want to do before the looping begins.
- step
update - after each loop
this expression is executed.
-
boolean test
- after the initialization, after
each loop and the step update, this Boolean expression
is executed. If it returns false,
the looping ends, if true
the looping continues.
-
statement
- this statement executes once
for each loop as long as the Boolean test is true. Can
be a single statement or a compound statement containing a code
block within parentheses.
A typical for
loop:
int
i, j=1;
for(i=0;
i<10; i++) j = j +1;
which follows these steps
- Assigns 0 to integer i
- Checks if i still less than 10
- Executes j = j + 1
- Executes the i++ operation
- If so then repeats from step 2,
- Otherwise, it ends the loop and proceeds to the next statement
This is the most common type for
loop but note that you can put any expressions you desire within
the for loop parentheses as long as the Boolean test returns a Boolean
value.
Here are some variations:
- Countdown:
int k, m;
for( k=10, m =1; k > 0; k--) m = m*k;
- Multiple expressions
in initialization
using comma to separate expressions:
for(int
i=0, j=0; i<10; i++ )
{
j = j*i;
j++;
}
- Multiple expressions in step update
using comma to separate expressions:
int k,m,n;
for( k=10; k > 0; k--, m++) n = m*k;
- Empty loop expressions:
for(; i<limit ;)
{ i = a.test();}
while
Statement
Suppose you want to do an indefinite number
of loops, stopping only when a particular condition occurs. It is
possible to do this with a for
loop and a break
statement (see flow control).
For example,
for
(;;)
{
...
if( i < 2) break;
}
next statement;
here the process will jump to the next statement following the
loop if the i<2
test is true.
However, this does not make for very elegant code. The while
statement provides a more readable option:
while(
test ) statement;
where statement will repeatedly execute
as long as the Boolean test expression returns true.
The statement can be a single statement or a compound statement
of multiple statements enclosed by parentheses:
int
i = 5, j=1;
while ( i >= 2 )
{
j = i*j;
i--;
}
do/while
Statement
The test for the while
statement occurs before the first loop through the code segment.
So if the test returns false
the first time, the statement will not execute.
Perhaps a situation occurs where you want
the code to execute at least once. Then you can use the do-while
statement:
do
statement;
while(
test );
where statement ( which, as usual,
can can consist of a compound statement with multiple statements
in parentheses) will execute at least once.
Note: The
semicolon at the end of the while(test);
is required.
For example,
int
i = 3;
do{
i = i - 2;
} while ( i < 2);
Enhanced
for loop (added with Java 5.0)
for
(type value : container) statement
This new feature of J2SE5.0 allows you to loop through a Collections
container or an array with the short compact statement as shown.
Here statement is executed for each element
in the container. (The enhanced loop is also referred to as the
"for-each loop.)
So the the following snippet:
int
[] an_array = {5, 6, 8, 10};
int cnt = 0;
for (int j : an_array) {
System.out.println ("an_array["
+ cnt +"] = " + j);
cnt++;
}
would give the following output:
an_array[0]
= 5
an_array[1] = 6
an_array[2] = 8
an_array[3] = 10
The enhanced for loop works nicely with the generics feature discussed
in Chapter 10 since all elements of a container are guaranteed in
that case to be of a single type.
See also the Chapter
10: Collections Framework and Chapter
10: Generics for more about these.
References & Web Resources
Latest update: Oct. 19, 2005
|