Liang Introduction to Java Programming Ninth Edition c





















































- Slides: 53
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. CHAPTER 4 LOOPS Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 2 Objectives F To write programs for executing statements repeatedly using a while loop (4. 2). F To follow the loop design strategy to develop loops (4. 2. 1– 4. 2. 3). F To control a loop with a sentinel value (4. 2. 4). F To write loops using do-while statements (4. 3). F To write loops using for statements (4. 4). F To discover the similarities and differences of three types of loop statements (4. 5). F To write nested loops (4. 6). F To implement program control with break and continue (4. 9). Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
3 Motivations Suppose that you need to print a string (e. g. , "Welcome to Java!") a hundred times. It would be tedious to have to write the following statement a hundred times: System. out. println("Welcome to Java!"); So, how do you solve this problem? Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
4 Opening Problem: 100 times System. out. println("Welcome to to to Java!"); Java!"); … … … System. out. println("Welcome to Java!"); Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
5 while Loop Flow Chart while (loop-continuation-condition) { // loop-body; int count = 0; while (count < 100) { System. out. println("Welcome to Java!"); Statement(s); } count++; } Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
6 Introducing while Loops Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
7 Tracing while Loops Initialize count Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
8 Introducing while Loops (count < 2) is true Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
9 Introducing while Loops Print counter value Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
10 Introducing while Loops Increase count by 1 count is 1 now Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
11 Introducing while Loops (count < 2) is still true since count is 1 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
12 Introducing while Loops Print counter value Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
13 Introducing while Loops The loop exits. Execute the next statement after the loop. When counter > 10 exit loop Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
14 Problem: Repeat Addition Until Correct Recall that Listing 3. 1 Addition. Quiz. java gives a program that prompts the user to enter an answer for a question on addition of two single digits. Using a loop, you can now rewrite the program to let the user enter a new answer until it is correct. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
15 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
16 Output Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
17 Problem: Guessing Numbers Write a program that randomly generates an integer between 0 and 100, inclusive. The program prompts the user to enter a number continuously until the number matches the randomly generated number. For each user input, the program tells the user whether the input is too low or too high, so the user can choose the next input intelligently. Here is a sample run: Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
18 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
19 Output Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
20 Caution Don’t use floating-point values for equality checking in a loop control. Since floating-point values are approximations for some values, using them could result in imprecise counter values and inaccurate results. Consider the following code for computing 1 + 0. 9 + 0. 8 +. . . + 0. 1: double item = 1; double sum = 0; while (item != 0) { // No guarantee item will be 0 sum += item; item -= 0. 1; } System. out. println(sum); Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
21 do-while Loop do { // Loop body; Statement(s); } while (loop-continuation-condition); Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
22 Example Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
23 for Loops for (initial-action; loop-continuationcondition; action-after-each-iteration) { // loop body; Statement(s); } int i; for (i = 0; i < 100; i++) { System. out. println( "Welcome to Java!"); } Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
24 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
25 for loops Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
26 Example Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
animation 27 Trace for Loop int i; for (i = 0; i < 2; i++) { System. out. println( "Welcome to Java!"); } Declare i Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
animation 28 Trace for Loop, cont. int i; for (i = 0; i < 2; i++) { System. out. println( "Welcome to Java!"); } Execute initializer i is now 0 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
animation 29 Trace for Loop, cont. int i; for (i = 0; i < 2; i++) { System. out. println( "Welcome to Java!"); } (i < 2) is true since i is 0 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
animation 30 Trace for Loop, cont. Print Welcome to Java int i; for (i = 0; i < 2; i++) { System. out. println("Welcome to Java!"); } Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
animation 31 Trace for Loop, cont. int i; for (i = 0; i < 2; i++) { System. out. println("Welcome to Java!"); } Execute adjustment statement i now is 1 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
animation 32 Trace for Loop, cont. int i; for (i = 0; i < 2; i++) { System. out. println("Welcome to Java!"); } (i < 2) is still true since i is 1 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
animation 33 Trace for Loop, cont. Print Welcome to Java int i; for (i = 0; i < 2; i++) { System. out. println("Welcome to Java!"); } Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
animation 34 Trace for Loop, cont. int i; for (i = 0; i < 2; i++) { System. out. println("Welcome to Java!"); } Execute adjustment statement i now is 2 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
animation 35 Trace for Loop, cont. int i; for (i = 0; i < 2; i++) { System. out. println("Welcome to Java!"); } (i < 2) is false since i is 2 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
animation 36 Trace for Loop, cont. int i; for (i = 0; i < 2; i++) { System. out. println("Welcome to Java!"); } Exit the loop. Execute the next statement after the loop Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
© 1992 -2010 by Pearson Education, Inc. All Rights Reserved. Examples Using the for Statement • a)Vary the control variable from 1 to 100 in increments of 1. • for ( int i = 1; i <= 100; i++ ) • b)Vary the control variable from 100 to 1 in decrements of 1. • for ( int i = 100; i >= 1; i-- ) • c)Vary the control variable from 7 to 77 in increments of 7. • for ( int i = 7; i <= 77; i += 7 ) Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
© 1992 -2010 by Pearson Education, Inc. All Rights Reserved. Examples Using the for Statement (Cont. ) • d)Vary the control variable from 20 to 2 in decrements of 2. • for ( int i = 20; i >= 2; i -= 2 ) • e)Vary the control variable over the values 2, 5, 8, 11, 14, 17, 20. • for ( int i = 2; i <= 20; i += 3 ) • f)Vary the control variable over the values 99, 88, 77, 66, 55, 44, 33, 22, 11, 0. • for ( int i = 99; i >= 0; i -= 11 ) Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
39 Note If the loop-continuation-condition in a for loop is omitted, it is implicitly true. Thus the statement given below in (a), which is an infinite loop, is correct. Nevertheless, it is better to use the equivalent loop in (b) to avoid confusion: Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
40 Caution Adding a semicolon at the end of the for clause before the loop body is a common mistake, as shown below: Logic Error for (int i=0; i<10; i++); { System. out. println("i is " + i); } Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
41 Caution, cont. Similarly, the following loop is also wrong: int i=0; while (i < 10); Logic Error { System. out. println("i is " + i); i++; } In the case of the do loop, the following semicolon is needed to end the loop. int i=0; do { System. out. println("i is " + i); i++; } while (i<10); Correct Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
42 Which Loop to Use? The three forms of loop statements, while, do-while, and for, are expressively equivalent; that is, you can write a loop in any of these three forms. For example, a while loop in (a) in the following figure can always be converted into the following for loop in (b): A for loop in (a) in the following figure can generally be converted into the following while loop in (b) except in certain special cases (see Review Question 3. 19 for one of them): Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
43 Recommendations Use the one that is most intuitive and comfortable for you. In general, a for loop may be used if the number of repetitions is known, as, for example, when you need to print a message 100 times. A while loop may be used if the number of repetitions is not known, as in the case of reading the numbers until the input is 0. A do-while loop can be used to replace a while loop if the loop body has to be executed before testing the continuation condition. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
44 Nested Loops Problem: Write a program that uses nested for loops to print a multiplication table. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
45 Output Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
46 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
© 1992 -2010 by Pearson Education, Inc. All Rights Reserved. break and continue Statements • The continue statement, when executed in a while, for or do…while, skips the remaining statements in the loop body and proceeds with the next iteration of the loop. • In while and do…while statements, the program evaluates the loop-continuation test immediately after the continue statement executes. • In a for statement, the increment expression executes, then the program evaluates the loop-continuation test. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
48 Using break and continue Examples for using the break and continue keywords: Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
© 1992 -2010 by Pearson Education, Inc. All Rights Reserved. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
© 1992 -2010 by Pearson Education, Inc. All Rights Reserved. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
51 Sentinel-controlled loops • A Sentinel value is a value that is used to end the iterations of a loop. • It can be impelmented in any kind of loops Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
52 Example • Develop a java program to determine the gross pay for a number of employees. The company pays “straight time” for the first 40 hours worked by each employee and pays “time-and-ahalf” for all hours worked in excess of 40 hours. Your program should input the number of hours each employee worked last week and the hourly rate of each employee. It then should determine and display the employee's gross pay. • Hint: Use -1 as a sentinel value Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
Control structures in Java Control Structure Sequence structure Selection Structure if If …. else Repetition structure Switch While do…while Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. for