Java Reference
In-Depth Information
The output generated by this program is shown here:
Notice one other thing in this program. The line
declares three variables,
a
,
b
, and
c
, by use of a comma-separated list. As mentioned earlier,
when you need two or more variables of the same type, they can be declared in one state-
ment. Just separate the variable names by commas.
The for Loop
You can repeatedly execute a sequence of code by creating a
loop
. Java supplies a powerful
assortment of loop constructs. The one we will look at here is the
for
loop. The simplest
form of the
for
loop is shown here:
for
(initialization; condition; iteration) statement;
In its most common form, the
initialization
portion of the loop sets a loop control variable
to an initial value. The
condition
is a Boolean expression that tests the loop control variable.
If the outcome of that test is true, the
for
loop continues to iterate. If it is false, the loop
terminates. The
iteration
expression determines how the loop control variable is changed
each time the loop iterates. Here is a short program that illustrates the
for
loop: