Programming and Problem Solving with Java

(やまだぃちぅ) #1
259

{

number++;
out.println(number);
}
9.By rearranging the order of the statements (don’t change the way they are writ-
ten), make the loop in Exercise 8 print the numbers from 1 through 10.

10.When the following code is executed, how many iterations of the loop are per-
formed?
number = 2 ;
done = false;
while( !done )
{
number = number * 2 ;
if(number > 64 )
done = true;
}


11.What is the output of this nested loop structure?


i = 4 ;
while(i >= 1 )
{
j = 2 ;
while(j >= 1 )
{
out.print(j + " ");
j--;
}
out.println(i);
i--;
}

12.The following code segment is supposed to write out the even numbers
between 1 and 15. (nis an intvariable.) It has two flaws in it.
n = 2 ;
while(n != 15 )
{
n = n + 2 ;
out.print(n + " ");
}
a.What is the output of the code as written?
b.Correct the code so that it works as intended.

Free download pdf