Java Reference
In-Depth Information
Self-Test Exercises
(continued)
d.
!(count == 12)
e .
(count == 1) && (x < y)
f .
(count < 10)
||
(x < y)
g .
!( ((count < 10)
||
(x < y)) && (count >= 0) )
h .
((limit/count) > 7)
||
(limit < 20)
i .
(limit < 20)
||
((limit/count) > 7)
j .
((limit/count) > 7) && (limit < 0)
k .
(limit < 0) && ((limit/count) > 7)
20. Does the following sequence produce a division by zero?
int
j = -1;
if
((j > 0) && (1/(j+1) > 10))
System.out.println(i);
21. Convert the following expression to an equivalent fully parenthesized expression:
bonus + day * rate / correctionFactor * newGuy - penalty
3.3
Loops
“Few tasks are more like the torture of Sisyphus than housework, with its
endless repetition: the clean becomes soiled, the soiled is made clean, over
and over, day after day.”
SIMONE DE BEAUVOIR
Looping mechanisms in Java are similar to those in other high-level languages. The
three Java loop statements are the
while
statement, the
do-while
statement, and
the
for
statement. The same terminology is used with Java as with other languages.
The code that is repeated in a loop is called the
body of the loop
. Each repetition of
the loop body is called an
iteration
of the loop.
body of
the loop
iteration
while
Statement and
do-while
Statement
The syntax for the
while
statement and its variant, the
do-while
statement, is given
later in this chapter in the box entitled “Syntax for
while
and
do-while
Statements.”
In both cases, the multistatement body is a special case of the loop with a single-
statement body. The multistatement body is a single compound statement. Examples
of
while
and
do-while
statements are given in Display 3.7 .
while
and
do-while
compared