Java Reference
In-Depth Information
choose the Run Java Application command and set the “Prompt for parameters” option for
it.
3
After you set this preference, when you run a program, a window will appear in which
you can enter options for the
java run
command—for example,
-enableassertions
.)
Self-Test Exercises
40. Fix the bug in the code in the subsection “Tracing Variables.”
41. Add some suitable output statements to the following code so that all variables
are traced:
int
n, sum = 0;
for
(n = 1; n < 10; n++)
sum = sum + n;
System.out.println("1 + 2 + ...+ 9 + 10 == " + sum);
42. What is the bug in the following code? What do you call this kind of loop bug?
int
n, sum = 0;
for
(n = 1; n < 10; n++)
sum = sum + n;
System.out.println("1 + 2 + ...+ 9 + 10 == " + sum);
43. Write an assertion check that checks to see that the value of the variable
time
is less
than or equal to the value of the variable
limit
. Both variables are of type
int
.
3
If you are running applets, you also need to select the “Prompt for parameters” option for the Run
Java Applet command on the Tools submenu.
Chapter Summary
•
The Java branching statements are the
if-else
statement and the
switch
statement.
•
A
switch
statement is a multiway branching statement. You can also form mul-
tiway branching statements by nesting
if-else
statements to form a multiway
if-else
statement.
•
Boolean expressions are evaluated similar to the way arithmetic expressions are
evaluated. The value of a Boolean expression can be saved in a variable of type
boolean
.
•
The Java loop statements are the
while
,
do-while
, and
for
statements.
•
A
do-while
statement always iterates its loop body at least one time. Both a
while
statement and a
for
statement might iterate its loop body zero times.
3
If you are running applets, you also need to select the “Prompt for parameters” option for the Run
Java Applet command on the Tools submenu.