Lab-Ch5

213 downloads 1466 Views 461KB Size Report
To use the break and continue program control statements to ... After reading Chapter 5 of Java How to Program: Seventh Edition, answer the given questions.
5 Not everything that can be counted counts, and not every thing that counts can be counted.

Control Statements: Part 2

—Albert Einstein

Who can control his fate? —William Shakespeare

OBJECTIVES

The used key is always bright.

In this chapter you will learn:

—Benjamin Franklin



Intelligence … is the faculty of making artificial objects, especially tools to make tools.





—Henri Bergson

Every advantage in the past is judged in the light of the final issue. —Demosthenes





The essentials of counter-controlled repetition. To use the for and do…while repetition statements to execute statements in a program repeatedly. To understand multiple selection using the switch selection statement. To use the break and continue program control statements to alter the flow of control. To use the logical operators to form complex conditional expressions in control statements.

© Copyright 1992-2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Chapter 5

Control Statements: Part 2

Assignment Checklist Name:

Date:

Section:

Exercises

Assigned: Circle assignments

Prelab Activities Matching

YES

NO

Fill in the Blank

13, 14, 15, 16, 17, 18, 19, 20, 21

Short Answer

22, 23, 24, 25, 26

Programming Output

27, 28, 29, 30, 31, 32, 33, 34, 35, 36

Correct the Code

37, 38, 39, 40

Lab Exercises Exercise 1 — Triangles Follow-Up Question and Activity Exercise 2 — Sales Follow-Up Questions and Activities Exercise 3 — Pythagorean Triples Follow-Up Questions and Activities Debugging

YES

NO

1 YES

NO

1, 2 YES

NO

1, 2, 3 YES

NO

Postlab Activities Coding Exercises

1, 2, 3, 4, 5, 6, 7, 8

Programming Challenges

1, 2

© Copyright 1992-2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Date Due

153

Chapter 5

Control Statements: Part 2

155

Prelab Activities Matching Name:

Date:

Section: After reading Chapter 5 of Java How to Program: Seventh Edition, answer the given questions. These questions are intended to test and reinforce your understanding of key Java concepts. You may answer these questions either before or during the lab. For each term in the left column, write the letter for the description that best matches the term from the right column. Term E H J A B F D L I G C K

Description

1. && 2. || 3. ! 4. switch 5. continue 6. break 7. for repetition statement 8. do…while repetition statement 9. | 10. off-by-one error 11. & 12. constant

a) Handles a series of decisions, in which a particular variable or expression is tested for values it can assume and different actions are taken. b) Skips any remaining statements in the body of a repetition statement and proceeds with the next iteration of the loop. c) Boolean logical AND d) Handles all of the details of counter-controlled repetition. e) Conditional AND. f)

Causes immediate exit from a repetition statement.

g) Can be caused by the use of an incorrect relational operator or using an incorrect final value of a loop counter in the condition of a repetition statement. h) Conditional OR. i)

Boolean logical inclusive OR.

j)

Logical negation.

k) A variable which contains a value which does not change for the entire program. l)

Repetition statement that tests the loop-continuation condition at the end of the loop, so that the body of the loop will execute at least once.

© Copyright 1992-2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Chapter 5

Control Statements: Part 2

Prelab Activities

157

Name:

Fill in the Blank Fill in the Blank

Name:

Date:

Section: Fill in the blanks for each of the following statements: 13. The %b format specifier causes the value of a boolean expression to be output as the word true or the word false based on the expression’s value. 14. Typically, for statements are used for counter-controlled repetition and while statements are used for sentinel-controlled repetition. 15. In most programs, it is necessary to include a(n) break statement after the statements for each case in a switch statement. 16. The scope of a variable defines where it can be referenced in a program by using just the variable’s name. 17. Logical operators may be used to form complex conditions by combining conditions. 18. Placing a semicolon after the header of a for statement is normally a(n) logic error. 19. Programs should control counting loops with integer values. 20. Infinite loops occur when the loop-continuation condition in a repetition statement never becomes false . 21. The expression in parentheses following keyword switch .

switch

is called the controlling expression of the

© Copyright 1992-2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Chapter 5

Control Statements: Part 2

Prelab Activities

159

Name:

Short Answer Short Answer

Name:

Date:

Section: Answer the following questions in the space provided. Your answers should be concise; aim for two or three sentences. 22. What is required to perform counter-controlled repetition? Counter-controlled repetition requires the name of a control variable, the initial value of the control variable, the increment or decrement by which the control variable is modified each time through the loop, and the condition that tests for the final value of the control variable. 23. Why should programs control counting loops with integers and not with floating-point numbers? Programs should control counting loops with integers because floating-point values may be approximate. Counting with floating-point values may result in imprecise counter values and inaccurate tests for termination. 24. Explain why placing a semicolon after the header of a for statement is a logic error and not a compilation error. Placing a semicolon after the for statement header is not a compilation error because Java treats the semicolon as an empty statement, which satisfies the requirement that the for statement have at least one statement in its body. This normally is a logic error because the code will compile, but will not execute correctly. 25. Differentiate between the while and the do…while repetition statements. In a while statement the program tests the loop-continuation condition at the beginning of the loop, before performing the body of the loop. If the initial condition is false, the body does not execute. The do…while statement tests the condition after performing the body of the loop, so the body of the loop always executes at least once. 26. Explain why an infinite loop can occur and how one can be prevented. An infinite loop occurs in a repetition statement when the loop-continuation condition never becomes false. In a counter-controlled loop, ensure that the control variable value is modified in a way that will eventually cause the loop-continuation condition to become false. In a sentinel-controlled loop ensure that the sentinel value is eventually input.

© Copyright 1992-2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Chapter 5

Control Statements: Part 2

Prelab Activities

161

Name:

Programming Output Programming Output

Name:

Date:

Section: For each of the given program segments, read the code and write the output in the space provided below each program. [Note: Do not execute these programs on a computer.] For the following questions, assume that the code segments are contained within the main method of a Java application. For questions 27–30, use the following code segment: 1 2 3 4 5 6

int startingValue; int terminatingValue; int stepValue; for ( int i = startingValue; i < terminatingValue; i += stepValue ) System.out.printf( "%d ", i );

27. What will be the output if the following code is placed at line 4 of the preceding code? 1 2 3

startingValue = 0; terminatingValue = 5; stepValue = 1;

Your answer: 0 1 2 3 4

28. What will be the output if the following code is placed at line 4 of the preceding code? 1 2 3

startingValue = -3; terminatingValue = 2; stepValue = 1;

Your answer: -3 -2 -1 0 1

© Copyright 1992-2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

162

Control Statements: Part 2

Chapter5

Prelab Activities

Name:

Programming Output 29. What will be the output if the following code is placed at line 4 of the preceding code? 1 2 3

startingValue = 6; terminatingValue = 5; stepValue = 1;

Your answer: No Output 30. What will be the output if the following code is placed at line 4 of the preceding code? 1 2 3

startingValue = 0; terminatingValue = 5; stepValue = 3;

Your answer: 0 3

For questions 31–35, use the following class definition: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

int startingValue; int terminatingValue; int stepValue; for ( int i = startingValue; i