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
It is not true that life is one damn thing after another—
It's one damn thing over and over.
EDNA ST. VINCENT MILLAY,
Letter to Arthur Darison Ficke, October 24, 1930
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
state-
ment. 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
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