Java Reference
In-Depth Information
• A condition using
&&
or
||
operators (p. 176) uses short-circuit evaluation (p. 178)—they're
evaluated only until it's known whether the condition is true or false.
•The
&
and
|
operators (p. 178) work identically to the
&&
and
||
operators but always evaluate
both operands.
• A simple condition containing the boolean logical exclusive OR (
^
; p. 179) operator is
true
if and
only if
one of its operands is
true
and the other is
false
. If both operands are
true
or both are
false
,
the entire condition is
false
. This operator is also guaranteed to evaluate both of its operands.
•The unary
!
(logical NOT; p. 179) operator “reverses” the value of a condition.
Self-Review Exercises
5.1
Fill in the blanks in each of the following statements:
a)
Typically,
statements are used for counter-controlled repetition and
statements for sentinel-controlled repetition.
b)
The
do
…
while
statement tests the loop-continuation condition
executing
the loop's body; therefore, the body always executes at least once.
c)
The statement selects among multiple actions based on the possible values
of an integer variable or expression, or a
String
.
d)
The statement, when executed in a repetition statement, skips the remaining
statements in the loop body and proceeds with the next iteration of the loop.
e)
The operator can be used to ensure that two conditions are
both
true before
choosing a certain path of execution.
f)
If the loop-continuation condition in a
for
header is initially
, the program
does not execute the
for
statement's body.
g)
Methods that perform common tasks and do not require objects are called
methods.
5.2
State whether each of the following is
true
or
false
. If
false
, explain why.
a)
The
default
case is required in the
switch
selection statement.
b)
The
break
statement is required in the last case of a
switch
selection statement.
c)
The expression
((x
>
y
)
&&
(a
<
b
)) is true if either
x
>
y
is true or
a
<
b
is true.
d)
An expression containing the
||
operator is true if either or both of its operands are true.
e)
The comma (
,
) formatting flag in a format specifier (e.g.,
%,20.2f
) indicates that a value
should be output with a thousands separator.
f)
To test for a range of values in a
switch
statement, use a hyphen (
-
) between the start
and end values of the range in a
case
label.
g)
Listing cases consecutively with no statements between them enables the cases to per-
form the same set of statements.
5.3
Write a Java statement or a set of Java statements to accomplish each of the following tasks:
a)
Sum the odd integers between 1 and 99, using a
for
statement. Assume that the integer
variables
sum
and
count
have been declared.
b)
Calculate the value of
2.5
raised to the power of
3
, using the
pow
method.
c)
Print the integers from 1 to 20, using a
while
loop and the counter variable
i
. Assume
that the variable
i
has been declared, but not initialized. Print only five integers per line.
[
Hint:
Use the calculation
i % 5
. When the value of this expression is 0, print a newline
character; otherwise, print a tab character. Assume that this code is an application. Use
the
System.out.println()
method to output the newline character, and use the
Sys-
tem.out.print('\t'
) method to output the tab character.]
d)
Repeat part (c), using a
for
statement.