while Loop Flow Chart int count = 0; while (loop-continuation-condition) { while (count < 100) { // loop-body; Chapter 4 Loops System.out.println("Welcome to Java!"); Statement(s); count++; } } count = 0; Loop false false Continuation (count < 100)? Condition? true true Statement(s) System.out.println("Welcome to Java!"); (loop body) count++; CS170 Introduction to Computer Science (A) (B) 1 4 animation Trace while Loop Motivations Initialize count Suppose that you need to print a string (e.g., "Welcome to int count = 0; Java!") a hundred times. while (count < 2) { System.out.println("Welcome to Java!"); System.out.println("Welcome to Java!"); count++; How do you solve this problem? } 2 5 animation Trace while Loop, cont. Objectives To use while, do-while, and for loop statements to (count < 2) is true int count = 0; control the repetition of statements ( §§ 4.2-4.4). while (count < 2) { To know the similarities and differences between three types of loops ( § 4.5). System.out.println("Welcome to Java!"); To write nested loops ( § 4.6). count++; To learn the techniques for minimizing numerical } errors ( § 4.7). To implement program control with break and continue ( § 4.9). (GUI) To control a loop with a confirmation dialog ( § 4.10). 3 6
animation animation Trace while Loop, cont. Trace while Loop, cont. Print Welcome to Java Print Welcome to Java int count = 0; int count = 0; while (count < 2) { while (count < 2) { System.out.println("Welcome to Java!"); System.out.println("Welcome to Java!"); count++; count++; } } 7 10 animation animation Trace while Loop, cont. Trace while Loop, cont. Increase count by 1 Increase count by 1 int count = 0; int count = 0; count is 1 now count is 2 now while (count < 2) { while (count < 2) { System.out.println("Welcome to Java!"); System.out.println("Welcome to Java!"); count++; count++; } } 8 11 animation animation Trace while Loop, cont. Trace while Loop, cont. (count < 2) is still true since (count < 2) is false since count is int count = 0; int count = 0; 2 now count is 1 while (count < 2) { while (count < 2) { System.out.println("Welcome to Java!"); System.out.println("Welcome to Java!"); count++; count++; } } 9 12
animation Trace while Loop Ending a Loop with a Sentinel Value The loop exits. Execute the next Often the number of times a loop is executed is not int count = 0; statement after the loop. predetermined. You may use an input value to signify the end while (count < 2) { of the loop. Such a value is known as a sentinel value . System.out.println("Welcome to Java!"); Write a program that reads and calculates the sum of an count++; unspecified number of integers. The input 0 signifies the end of } the input. SentinalValue.java 13 16 Problem: Guessing Numbers do-while Loop Write a program that randomly generates an integer between 0 and 100, inclusive. The program prompts the user to enter a number. The program then tells the user whether the input is correct, too low or too high. Statement(s) (loop body) Modify the above program so that it prompts the user to Loop enter a number continuously until the number matches the true Continuation randomly generated number. For each user input, the do { Condition? program tells the user whether the input is too low or too // Loop body; false high, so the user can choose the next input intelligently. Statement(s); GuessNumberOneTime.java } while (loop-continuation-condition); GuessNumber.java 14 17 Problem: An Advanced Math Learning Tool Sentinel Example Use do-while loop to rewrite the program that reads and The Math subtraction learning tool program generates just calculates the sum of an unspecified number of integers one question for each run. You can use a loop to generate The input 0 signifies the end of the input. questions repeatedly. This example gives a program that generates five questions and reports the number of the correct answers after a student answers all five questions. SentinelValueDoWhile.java SubtractionQuizLoop.java 15 18
animation for Loops Trace for Loop, cont. for (initial-action; loop- for (int i = 0; i < 100; i++) { (i < 2) is true continuation-condition; System.out.println( int i; since i is 0 action-after-each-iteration) { "Welcome to Java!"); for (i = 0; i < 2; i++) { // loop body; } System.out.println( "Welcome to Java!"); Statement(s); } } Initial-Action i = 0 Loop false false Continuation (i < 100)? Condition? true true Statement(s) System.out.println( (loop body) "Welcome to Java"); Action-After-Each-Iteration i++ 19 22 (A) (B) animation animation Trace for Loop Trace for Loop, cont. Print Welcome to Java Declare i int i; int i; for (i = 0; i < 2; i++) { for (i = 0; i < 2; i++) { System.out.println( System.out.println("Welcome to Java!"); "Welcome to Java!"); } } 20 23 animation animation Trace for Loop, cont. Trace for Loop, cont. Execute adjustment statement Execute initializer int i; int i; i now is 1 i is now 0 for (i = 0; i < 2; i++) { for (i = 0; i < 2; i++) { System.out.println( System.out.println("Welcome to Java!"); "Welcome to Java!"); } } 21 24
animation animation Trace for Loop, cont. Trace for Loop, cont. (i < 2) is still true (i < 2) is false int i; since i is 1 int i; since i is 2 for (i = 0; i < 2; i++) { for (i = 0; i < 2; i++) { System.out.println("Welcome to Java!"); System.out.println("Welcome to Java!"); } } 25 28 animation animation Trace for Loop, cont. Trace for Loop, cont. Print Welcome to Java Exit the loop. Execute the next int i; int i; statement after the loop for (i = 0; i < 2; i++) { for (i = 0; i < 2; i++) { System.out.println("Welcome to Java!"); System.out.println("Welcome to Java!"); } } 26 29 animation Note Trace for Loop, cont. Execute adjustment statement The initial-action and action-after-each-iteration in a for int i; i now is 2 loop can be a list of zero or more comma-separated for (i = 0; i < 2; i++) { expressions. System.out.println("Welcome to Java!"); } for (int i = 1; i < 100; System.out.println(i++)); for (int i = 0, j = 0; (i + j < 10); i++, j++) { // Do something } 27 30
Minimizing Numerical Errors Note If the loop-continuation-condition in a for loop is omitted, • Floating-point numbers are represented it is implicitly true. approximately in the computer • Avoid using floating point numbers in the loop condition for ( ; ; ) { while (true) { Example: compute the sum for a series of Equivalent // Do something // Do something numbers: 0.01, 0.02, …, 1.0. } } (a) (b) TestSum.java Better! 31 34 Compare floating-point numbers Caution Adding a semicolon at the end of the for clause before Don’t use == or != to compare floating-point numbers the loop body is a common mistake, as shown below: Logic double data = Math.pow(Math.sqrt(2), 2) - 2; Error if (data == 0) // wrong for (int i=0; i<10; i++); System.out.println("data is zero"); { else System.out.println("i is " + i); System.out.println("data is not zero"); } 32 35 Caution, cont. Comparing Floating-Point Numbers int i=0; while (i < 10); To compare floating-point numbers test whether Logic Error { they are close enough : System.out.println("i is " + i); | x - y | ≤ ε i++; } where ε is a small number such as 10 -14 int i=0; final double EPSILON = 1E-14; do { if (Math.abs(x - y) <= EPSILON) System.out.println("i is " + i); // x is approximately equal to y i++; Correct } while (i<10); 33
Recommend
More recommend