Java Reference
In-Depth Information
277
Exercise R6.3. How often do the following loops execute? Assume that
i
is an integer variable that is not changed in the loop body.
a.
for (i = 1; i <= 10; i++) È
b.
for (i = 0; i < 10; i++) È
c.
for (i = 10; i > 0; iÎÎ) È
d.
for (i = Ċ10; i <= 10; i++) È
e.
for (i = 10; i > = 0; i++) È
f.
for (i = -10; i <= 10; i = i + 2) È
g.
for (i = -10; i <= 10; i = i + 3) È
Exercise R6.4. Rewrite the following
for
loop into a
while
loop.
int s = 0;
for (int i = 1; i <= 10; i++) s = s + i;
Exercise R6.5. Rewrite the following
do
loop into a
while
loop.
int n = 1;
double x = 0;
double s;
do
{
s = 1.0 / (n * n);
x = x + s;
n++;
}
while (s > 0.01);
Exercise R6.6. What is an infinite loop? On your computer, how can you
terminate a program that executes an infinite loop?
Exercise R6.7 Give three strategies to implement the following Ȓloop
and a halfȓ:
loop
{