Java Reference
In-Depth Information
APPENDIX
D
Java control structures
D.1
Control structures
Control structures affect the order in which statements are executed. There are two main catego-
ries:
selection statements
and
loops
.
A selection statement provides a decision point at which a choice is made to follow one route
through the body of a method or constructor rather than another route. An
if-else statement
in-
volves a decision between two different sets of statements, whereas a
switch statement
allows
the selection of a single option from among several.
Loops offer the option to repeat statements, either a definite or an indefinite number of times.
The former is typified by the
for-each loop
and
for loop
, while the latter is typified by the
while
loop
and
do loop
.
In practice, it should be borne in mind that exceptions to the above characterizations are quite
common. For instance, an
if-else statement
can be used to select from among several alternative
sets of statements if the
else
part contains a nested if-else statement, and a for loop can be used
to loop an indefinite number of times.
D.2
Selection statements
D.2.1 if-else
The
if-else statement
has two main forms, both of which are controlled by the evaluation of a
boolean expression:
if
(
expression
) {
if
(
expression
) {
statements
statements
} }
else
{
statements
}
In the first form, the value of the boolean expression is used to decide whether or not to execute
the statements. In the second form, the expression is used to choose between two alternative
sets of statements, only one of which will be executed.
Search WWH ::
Custom Search