77范文网 - 专业文章范例文档资料分享平台

java 课后答案04review

来源:网络收集 时间:2019-03-03 下载这篇文档 手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:或QQ: 处理(尽可能给您提供完整文档),感谢您的支持与谅解。点击这里给我发消息

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

百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库java 课后答案04review在线全文阅读。

java 课后答案04review.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印 下载失败或者文档不完整,请联系客服人员解决!
本文链接:https://www.77cn.com.cn/wenku/zonghe/489292.html(转载请注明文章来源)
Copyright © 2008-2022 免费范文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ: 邮箱:tiandhx2@hotmail.com
苏ICP备16052595号-18
× 注册会员免费下载(下载后可以自由复制和排版)
注册会员下载
全站内容免费自由复制
注册会员下载
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: