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 <0I 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 < 0 || 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