Java Reference
In-Depth Information
Self-Test Exercises
(continued)
35. For each of the following situations, tell which type of loop (
while
,
do-while
,
or
for
) would work best:
a. Summing a series, such as
1/2 + 1/3 + 1/4 + 1/5 + . . . 1/10
.
b. Reading in the list of exam scores for one student.
c. Reading in the number of days of sick leave taken by employees in a
department.
36. What is the output of the following?
int
number = 10;
while
(number > 0)
{
System.out.println(number);
number = number + 3;
}
37. What is the output of the following?
int
n, limit = 10;
for
(n = 1; n < limit; n++)
{
System.out.println("n == " + n);
System.out.println("limit == " + limit);
limit = n + 2;
}
38. What is the output produced by the following?
int
number = 10;
while
(number > 0)
{
number = number - 2;
if
(number == 4)
break
;
System.out.println(number);
}
System.out.println("The end.");
39. What is the output produced by the following?
int
number = 10;
while
(number > 0)
{
number = number - 2;
if
(number == 4)
continue
;
System.out.println(number);
}
System.out.println("The end.");