java 课后答案04review

2019-03-03 12:50

Chapter 4 Loops

1. count < 100 always true at Point A. count < 100 always false at Point C. count < 100 is sometimes true or sometimes false at Point B.

3. (a) Infinite number of times.

(b) Infinite number of times.

(c) The loop body is executed nine times. The printout is 3, 5, 7, 9 on

separate lines.

4. The difference between a do-while loop and a while loop is the order of evaluating the continuation-condition and executing the loop body. In a while loop, the continuation-condition is checked and then, if true, the loop body is executed. In a do-while loop, the loop body is executed for the first time before the continuation-condition is evaluated.

int sum = 0; int number; do { number = input.nextInt(); sum += number; } while (number != 0);

It would be wrong if it is initialized to a value between 0 and 100, because it could be the number you attempt to guess.

2.

5. 6.

Same. When the i++ and ++i are used in isolation, their effects are same. The three parts in a for loop control are as follows:

The first part initializes the control variable.

The second part is a Boolean expression that determines whether the loop will repeat.

The third part is the adjustment statement, which adjusts the control variable.

for (int i=1; i<=100; i++) System.out.println(i);

7.

max is 5 number 0

8.

sum is 14 count is 5

9.

max is 5 number 0

12.

13.

while loop:

long sum = 0; int i=0;

while (i<=1000) { sum += i++; }

do-while loop: long sum = 0; int i = 0; do {

sum += i++; }

while (i <= 1000);

No. Try n1 = 3 and n2 =3.

The keyword break is used to exit the current loop. The program in (A) will terminate. The output is Balance is 1.

The keyword continue causes the rest of the loop body to be skipped for the current iteration. The while loop will not terminate in (B). Yes.

for (int i=1; sum < 10000; i++) sum = sum + i;

Yes. The advantages of for loops are simplicity and readability. Compilers can produce more efficient code for the for loop than for the corresponding while loop.

10. 11.

The loop keeps doing something indefinitely. No. The scope of the variable is inside the loop.

14.

15.

16.

17. If a continue statement is executed inside a for loop, the rest of the iteration is skipped, then the action-after-each-iteration is performed and the loop-continuation-condition is checked. If a continue statement is executed inside a while loop, the rest of the iteration is skipped, then the loop-continuation-condition is checked.

Here is the fix:

int i = 0;

while (i < 4) {

if (i % 3 == 0) { i++;

continue; }

sum += i; i++; }

18. class TestBreak {

public static void main(string[]args) { int sum = 0; int number = 0;

while (number < 20 && sum < 100) { number++;

sum += number; }

System.out.println(\ } }

class TestContinue {

public static void main(String[] args) { int sum = 0; int number = 0;

while (number < 20) { number++;

if (number != 10 && number != 11) sum += number; }

System.out.println(\ } }

19. After the break statement is executed, the last println statement is executed. The output of this fragment is

1

2 1 2 2 3

20. After the continue statement is executed, the statement

j++

will be executed. The output of this fragment is

1 2 1 2 2 3

21.

Line 2: missing static.

Line 3: The semicolon (;) at the end of the for loop heading should be removed. Line 4: sum not defined.

Line 6: the semicolon (;) at the end of the if statement should be removed. Line 6: j not defined.

Line 7: Missing a semicolon for the first println statement.

Line 11: The semicolon (;) at the end of the while heading should be removed. Line 18: Missing a semicolon at the end of the do-while loop.

22.

for (int i = 0; i < 10; i++);

23.

Tip for tracing programs:

Draw a table to see how variables change in the program. Consider (a) for example.

i j output 1 0 0 1 1

2 0 0 2 1 1 2 2 3 0 0 3 1 1 3 2 2

(B) Line 3: The ; at the end of for loop should be removed. (A) compile error: i is not initialized.

3 3 4 0 0 4 1 1 4 2 2 4 3 3 4 4

(A).

0 0 1 0 1 2 0 1 2 3

(B).

**** **** 2 **** 3 2 **** 4 3 2 ****

(C).

1xxx2xxx4xxx8xxx16xxx 1xxx2xxx4xxx8xxx

1xxx2xxx4xxx 1xxx2xxx

1xxx

(D).

1G 1G3G 1G3G5G 1G3G5G7G 1G3G5G7G9G

24.

x is -2147483648

The reason:

When a variable is assigned a value that is too large (in size) to be stored, it causes overflow.

2147483647 + 1 is actually -2147483648

25.

(A)

n times (B)

n+1 times

(C)

n-5 times (D)

The ceiling of (n-5)/3 times


java 课后答案04review.doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:小班科学《生蛋熟蛋大比拼》活动反思

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: