Java Reference
In-Depth Information
}
}
This uses one particular kind of loop — called a for loop. The for loop statement on the third line causes
the statements in the following block to be repeated eight times. The number of times it is to be repeated is
determined by the stuff between parentheses following the keyword for — you will see how in a moment.
The point is that you could, in theory, repeat the same block of statements as many times as you want —
a thousand or a million or a billion — it is just as easy and it doesn't require any more lines of code. The
primary purpose of the for loop is to execute a block of statements a given number of times.
In general, a loop has two parts to it: it has a loopbody , which is the code that is to be repeated and can be
a single statement or a block of statements, and it has a loop control mechanism that determines how many
times the loop body should execute.
Varieties of Loop
There are four kinds of loop statements you can use. I introduce these in outline first to give an overview of
all the possibilities.
1. The Numerical for Loop
for (initialization_expression ; loop_condition ; increment_expression) {
// statements
}
I have described this loop as the numerical for loop as a rough indication of how it is used and to dis-
tinguish it from another variety of for loop that I describe in a moment. The numerical for loop is usually
just referred to as a for loop. The loop body for this loop is the block of statements between the braces. The
braces are optional when the loop body is just a single statement. The code to control the for loop appears
in parentheses following the keyword for .
As you can see, the loop control mechanism has three parts separated by semicolons. The first part, the
initialization_expression , executes once before the loop starts. You typically use this expression to
initialize a counter for the number of loop iterations — for example, i = 0 . With a loop controlled by a
counter, which can be an integer or a floating-point variable, you typically count up or down by whatever
increment or decrement you choose until the variable reaches some defined limit.
Execution of this loop continues as long as the condition you specify in the second part of the control
mechanism, the loop_condition , is true . This expression is checked at the beginning of each loop iter-
ation, and as long as it is true , the loop body executes. When loop_condition is false , the loop ends
and execution continues with the statement following the loop block. For example, if you use i<10 as the
loop_condition expression, the loop continues as long as the variable i has a value less than 10. The third
part of the control information between the parentheses, the increment_expression , is usually used to in-
crement the loop counter. This is executed at the end of each loop iteration. This could be ++i , which would
increment the loop counter, i , by one. Of course, you might want to increment the loop counter in steps
other than 1. For example, you might write i += 2 as the increment_expression to go in steps of 2, or even
something more complicated such as i = 2*i+1 .
2. The Collection-Based For Loop
Search WWH ::




Custom Search