Java Reference
In-Depth Information
where
test
indicates a
boolean
expression. If
test
evaluates to
true
, then
statement
is executed.
Similarly, the conditional
if-else
statement evaluates a
boolean
expres-
sion to decide which of two statements to evaluate, as in
if (test)
statement1;
else
statement2;
The statement(s) to evaluate can consist of single, one-line statements or of code
blocks of multiple statements enclosed in braces.
A sequence of conditions can be tested with multiple
if
tests, as in
if (test1)
statement1;
else if (test2)
statement2;
else if (test3)
statement3;
else
statement4;
2.9.3 Flow control statements
Several types of statements affect the flow or sequence of processing such as
repeating a section of code or jumping over a section of code. Such flow control
statements are essential tools for any type of programming. The following loop
statements repeat the processing of a statement or code block for a number of
times as set by a logic test.
2.9.3.1 The
for
loop
The
for
loop goes as follows:
for (start; test; action) statement;
Here
statement
repeatedly executes until the expression
test
returns
false
.
The loop begins with an evaluation of the
start
and
test
expressions and, after
each loop, the
action
expression is evaluated before
test
is evaluated again.
A typical example goes as
for (int i=0; i < 10; i++) j = i*j;
This begins with the declaration of the integer
i
initialized to 0 followed by the
evaluation of the test
i
<
10
. Since the variable
i
is less than 10, the evaluation of
the
j
=
i*j
statement proceeds. The processing “loops back” and evaluates
the
i++
expression and then the test is evaluated again. The looping continues
until the
test
expression returns
false
.
Search WWH ::
Custom Search