Chapter 7 Repetition Statements 2000 McGraw-Hl‖ Introduction to Object-Oriented Programming with Java-Wu Chapter 7-1
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 7 - 1 Chapter 7 Repetition Statements
Chapter 7 objectives After you have read and studied this chapter, you should be able to e Implement repetition control in a program using while statements e Implement repetition control in a program using do-while statements e Implement repetition control in a program using for statements e Nest a loop repetition statement inside another repetition statement Choose the appropriate repetition control statement for a given task e Prompt the user for a yes-no reply using the ResponseBox class from the javabook package e Output formatted data using the Format class from the javabook package. (Optional) Write simple recursive methods C 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 7-2
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 7 - 2 Chapter 7 Objectives After you have read and studied this chapter, you should be able to Implement repetition control in a program using while statements. Implement repetition control in a program using do–while statements. Implement repetition control in a program using for statements. Nest a loop repetition statement inside another repetition statement. Choose the appropriate repetition control statement for a given task. Prompt the user for a yes–no reply using the ResponseBox class from the javabook package. Output formatted data using the Format class from the javabook package. (Optional) Write simple recursive methods
The while statement int sum =0, number while( number <=100) Sum sum number; These statements are executed as long as number is less than or number= number 1 equal to 100 C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 7-3
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 7 - 3 The while Statement int sum = 0, number = 1; while ( number <= 100 ) { sum = sum + number; number = number + 1; } These statements are executed as long as number is less than or equal to 100
Syntax for the while statement while( Boolean Expression while(: number < 100 ··················································· 。sum sum number Statement (loop body) ∵ number number 1 C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 7-4
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 7 - 4 while ( number ) Statement (loop body) Boolean Expression
Control flow of while int sum =0. number= 1 number < true 100? false sum sum number number number 1 C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 7-5
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 7 - 5 Control Flow of while int sum = 0, number = 1 number <= 100 ? false sum = sum + number; number = number + 1; true
More Examples int sum Keeps adding the bers.2.3 .. until while( sum <=1000000)( the sum becomes larger than1.000.000 sum sum number umb umber 1 2 int product 1, numbe count 20, lastNumberi Computes the product of the first 20 odd integers lastNumber =2 while(number < lastNumber)( product product numberi number 2; C 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 7-6
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 7 - 6 More Examples Keeps adding the numbers 1, 2, 3, … until the sum becomes larger than 1,000,000. Computes the product of the first 20 odd integers. int sum = 0, number = 1; while ( sum <= 1000000 ) { sum = sum + number; number = number + 1; } 1 int product = 1, number = 1, count = 20, lastNumber; lastNumber = 2 * count - 1; while (number <= lastNumber) { product = product * number; number = number + 2; } 2
Example: Testing Input data lere's a, realistic example of using the while loop to accept only he va Input data r Accepts age between 0 and 130, exclusively Priming Read age inputBox getInteger("Your Age (between 0 and 130): hile(age 130) messageBox. show("An invalid age was entered.+ Please try again. age inputBox getInteger(" Your Age (between 0 and 130): )i C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 7-7
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 7 - 7 age = inputBox.getInteger("Your Age (between 0 and 130):"); while (age 130) { messageBox.show("An invalid age was entered. " + "Please try again."); age = inputBox.getInteger( "Your Age (between 0 and 130):" ); } Example: Testing Input Data Here’s a realistic example of using the while loop to accept only the valid input data. Accepts age between 0 and 130, exclusively. Priming Read
while Loop pitfall -1 int product =0 while( product 500000)i product= product 5i Infinite Loops Both loops will not terminate because the 2) int count =1 boolean expressions will never become false while( count count count 2 C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 7-8
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 7 - 8 while Loop Pitfall - 1 Infinite Loops Both loops will not terminate because the boolean expressions will never become false. int count = 1; while ( count != 10 ) { count = count + 2; } 2 int product = 0; while ( product < 500000 ) { product = product * 5; } 1
while Loop pitfall -2 float count =0. 0f while( count ! 1 0f ) count count +0.3333333f //seven 3s Using Real Numbers Loop 2 terminates, but Loop 1 does not because only an approximation of a real 2) float count =0.0f number can be stored in a computer memory while( count ! 1 0f ) count count +0.33333333f //eight 3s C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 7-9
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 7 - 9 while Loop Pitfall - 2 Using Real Numbers Loop 2 terminates, but Loop 1 does not because only an approximation of a real number can be stored in a computer memory. float count = 0.0f; while ( count != 1.0f ) { count = count + 0.33333333f; } //eight 3s 2 float count = 0.0f; while ( count != 1.0f ) { count = count + 0.3333333f; } //seven 3s 1
while Loop pitfall -3 r Goal: Execute the loop body 10 times ① count 1 (2 count 1 while( count 10) while( count <= 10) count++ count++i 3 count o 4 count=o while( count < 10 while( count 10) count++i coun七++; 1 and (3) exhibit off-by-one error C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 7-10
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 7 - 10 while Loop Pitfall - 3 Goal: Execute the loop body 10 times. count = 1; while ( count < 10 ) { . . . count++; } 1 count = 0; while ( count <= 10 ) { . . . count++; } 3 count = 1; while ( count <= 10 ) { . . . count++; } 2 count = 0; while ( count < 10 ) { . . . count++; } 4 1 and 3 exhibit off-by-one error