5Control Statements: Part 2;
Logical Operators
Objectives
In this chapter you’ll:
Learn the essentials of
counter-controlled iteration.
Use the for and dowhile
iteration statements to
execute statements in a
program repeatedly.
Understand multiple selection
using the switch selection
statement.
Self-Review Exercises 2
Self-Review Exercises
5.1 Fill in the blanks in each of the following statements:
a) Typically, statements are used for counter-controlled iteration and
statements for sentinel-controlled iteration.
b) The dowhile statement tests the loop-continuation condition executing
the loop’s body; therefore, the body always executes at least once.
c) The statement selects among multiple actions based on the possible values of
an integer variable or expression, or a String.
d) The statement, when executed in an iteration statement, skips the remaining
statements in the loop body and proceeds with the next iteration of the loop.
e) The operator (with short-circuit evaluation) can be used to ensure that two
conditions are both true before choosing a certain path of execution.
f) If the loop-continuation condition in a for header is initially , the program
does not execute the for statement’s body.
g) Methods that perform common tasks and do not require objects are methods.
5.2 State whether each of the following is true or false. If false, explain why.
a) The default case is required in the switch selection statement.
b) The break statement is required in the last case of a switch selection statement.
c) The expression ((x > y) && (a < b)) is true if either x > y is true or a < b is true.
d) An expression containing the || operator is true if either or both of its operands are true.
e) The comma (,) formatting flag in a format specifier (e.g., %,20.2f) indicates that a value
f) To test for a range of values in a switch statement, use a hyphen () between the start
and end values of the range in a case label.
g) Listing cases consecutively with no statements between them enables the cases to per-
form the same set of statements.
5.3 Write a Java statement or a set of Java statements to accomplish each of the following tasks:
a) Sum the odd integers between 1 and 99, using a for statement. Assume that the integer
variables sum and count have been declared.
ANS:
5.4 Find the error in each of the following code segments, and explain how to correct it:
a)
b)
c)
d) The following code should print the values 1 to 10:
1i = 1;
2while (i <= 10);
3 ++i;
4}
1for (k = 0.1; k != 1.0; k += 0.1) {
2 System.out.println(k);
3}
1switch (n) {
2 case 1:
3 System.out.println(“The number is 1”);
4 case 2:
5 System.out.println(“The number is 2”);
6 break;
7 default:
8 System.out.println(“The number is not 1 or 2”);
9 break;
10 }
1n = 1;
2while (n < 10) {
3 System.out.println(n++);
4}
© 2018 Pearson Education, Inc., 330 Hudson Street, NY NY 10013. All rights reserved.
5Chapter 5 Control Statements: Part 2; Logical Operators
Exercises
NOTE: Solutions to the programming exercises are located in the ch05solutions folder.
Each exercise has its own folder named ex05_## where ## is a two-digit number representing
the exercise number. For example, exercise 5.11’s solution is located in the folder ex05_11.
5.5 Describe the four basic elements of counter-controlled iteration.
5.6 Compare and contrast the while and for iteration statements.
5.7 Discuss a situation in which it would be more appropriate to use a dowhile statement
than a while statement. Explain why.
5.8 Compare and contrast the break and continue statements.
Exercises 6
5.9 Find and correct the error(s) in each of the following segments of code:
a)
7Chapter 5 Control Statements: Part 2; Logical Operators
ANS:
5.13 (Factorials) Factorials are used frequently in probability problems. The factorial of a positive
integer n (written n! and pronouncedn factorial”) is equal to the product of the positive integers from
1 to n. Write an application that calculates the factorials of 1 through 20. Use type long. Display the
results in tabular format. What difficulty might prevent you from calculating the factorial of 100?
5.19 Assume that i = 1, j = 2, k = 3 and m = 2. What does each of the following statements print?
a) System.out.println(i == 1);
b) System.out.println(j == 3);
c) System.out.println((i >= 1) && (j < 4));
d) System.out.println((m <= 99) & (k < m));
e) System.out.println((j >= i) || (k == m));
f) System.out.println((k + m < j) | (3 – j >= k));
g) System.out.println(!(k > m));
5.20 (Calculating the Value of π) Calculate the value of π from the infinite series
Print a table that shows the value of π approximated by computing the first 200,000 terms of this
series. How many terms do you have to use before you first get a value that begins with 3.14159?
5.26 A criticism of the break statement and the continue statement is that each is unstructured.
Actually, these statements can always be replaced by structured statements, although doing so can be
awkward. Describe in general how you’d remove any break statement from a loop in a program and
replace it with some structured equivalent. [Hint: The break statement exits a loop from the body of
the loop. The other way to exit is by failing the loop-continuation test. Consider using in the loop-
continuation test a second test that indicates “early exit because of a ‘break’ condition.”] Use the tech-
nique you develop here to remove the break statement from the application in Fig. 5.13.
π44
3
4
5
4
7
4
9
4
11
—–
++ +=
© 2018 Pearson Education, Inc., 330 Hudson Street, NY NY 10013. All rights reserved.
Exercises 8
5.27 What does the following program segment do?
1for (i = 1; i <= 5; i++) {
2 for (j = 1; j <= 3; j++) {
3 for (k = 1; k <= 4; k++) {
4 System.out.print(‘*’);
5 }
6
7 System.out.println();
8 }
9
10 System.out.println();
11 }