The while Statement int sum 0 number 1
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. } © 2000 Mc. Graw-Hill Introduction to Object-Oriented Programming with Java--Wu 1
Syntax for the while Statement while ( <boolean expression> ) { <statement> } Boolean Expression while ( sum Statement (loop body) number <= 100 = ) { sum + number; number = number + 1; } © 2000 Mc. Graw-Hill Introduction to Object-Oriented Programming with Java--Wu 2
Control Flow of while int sum = 0, number = 1 number <= 100 ? false true sum = sum + number; number = number + 1; © 2000 Mc. Graw-Hill Introduction to Object-Oriented Programming with Java--Wu 3
Example Programs Puuuurginooo … out to reality … While 10000. java Puuuurginooo … out to reality … While. Input. java © 2000 Mc. Graw-Hill Introduction to Object-Oriented Programming with Java--Wu 4
while Loop Pitfall - 1 1 int product = 0; while ( product < 500000 ) { product = product * 5; } Infinite Loops 2 int count = 1; Both loops will not terminate because the boolean expressions will never become false. while ( count != 10 ) { count = count + 2; } © 2000 Mc. Graw-Hill Introduction to Object-Oriented Programming with Java--Wu 5
while Loop Pitfall - 2 1 float count = 0. 0 f; while ( count != 1. 0 f ) { count = count + 0. 3333333 f; } Using Real Numbers 2 float count = 0. 0 f; Loop 2 terminates, but Loop 1 does not because only an approximation of a real number can be stored in a computer memory. while ( count <= 1. 0 f ) { count = count + 0. 3333333 f; } © 2000 Mc. Graw-Hill Introduction to Object-Oriented Programming with Java--Wu 6
while Loop Pitfall - 3 Goal: Execute the loop body 10 times. 1 2 count = 1; while (count < 10) { while (count <= 10) { . . . count++; } 3 count = 1; } 4 count = 0; while (count <= 10) { count = 0; while (count < 10) { . . . count++; } } 1 and 3 exhibit off-by-one error. © 2000 Mc. Graw-Hill Introduction to Object-Oriented Programming with Java--Wu 7
Example Programs Grunk … out to reality … While. Log. java Grunk … out to reality … While. Boolean. java © 2000 Mc. Graw-Hill Introduction to Object-Oriented Programming with Java--Wu 8
The do-while Statement int sum = 0, number = 1; do { sum += number; number++; These statements are executed as long as sum is less than or equal to 1, 000. } while ( sum <= 1000000 ); © 2000 Mc. Graw-Hill Introduction to Object-Oriented Programming with Java--Wu 9
Syntax for the do-while Statement do { <statement> } while (<boolean expression>); do { sum += number; number++; Statement (loop body) } while (sum <= 1000000); Boolean Expression © 2000 Mc. Graw-Hill Introduction to Object-Oriented Programming with Java--Wu 10
Control Flow of do-while int sum = 0, number = 1 sum += number; number++; sum <= 1000000 ? true false © 2000 Mc. Graw-Hill Introduction to Object-Oriented Programming with Java--Wu 11
Example Programs Orque … out to reality … Do. While. Input. java Orque … out to reality … Do. While. Drink. java © 2000 Mc. Graw-Hill Introduction to Object-Oriented Programming with Java--Wu 12
Pre-test vs. Post-test loops Use a pre-test loop for something that may be done zero times Use a post-test for something that is always done at least once © 2000 Mc. Graw-Hill Introduction to Object-Oriented Programming with Java--Wu 13
Checklist for Repetition Control 1. Watch out for the off-by-one error (OBOE). 2. Make sure the loop body contains a statement that will eventually cause the loop to terminate. 3. Make sure the loop repeats exactly the correct number of times. © 2000 Mc. Graw-Hill Introduction to Object-Oriented Programming with Java--Wu 14
The for Statement i, sum = 0, number; for (i = 0; i < 20; i++) { number = input. Box. get. Integer(); sum += number; } © 2000 Mc. Graw-Hill These statements are executed for 20 times ( i = 0, 1, 2, … , 19). Introduction to Object-Oriented Programming with Java--Wu 15
Syntax for the for Statement for ( <initialization>; <boolean expression>; <update> ) <statement> Boolean Expression Initialization for ( i = 0 ; i < 20 Update ; number = input. Box. get. Integer(); sum += number; i++ ) { Statement (loop body) } © 2000 Mc. Graw-Hill Introduction to Object-Oriented Programming with Java--Wu 16
Control Flow of for i = 0; false i < 20 ? true number = input. Box. get. Integer( ); sum += number; i ++; © 2000 Mc. Graw-Hill Introduction to Object-Oriented Programming with Java--Wu 17
Example Programs Poodyplat … out to reality … For. Print. java Poodyplat … out to reality … For. Power. java Poodyplat … out to reality … For. Fibonacci. java © 2000 Mc. Graw-Hill Introduction to Object-Oriented Programming with Java--Wu 18
The Nested-for Statement Nesting a for loop inside another is a common technique Generate the following table using nested-for statements. For. Table. java © 2000 Mc. Graw-Hill Introduction to Object-Oriented Programming with Java--Wu 19
Indefinite vs. Definite loops For loops and while loops are exchangeable but Use a for loop when the number of iterations is definite Use a while or do-while when the number of iterations depends on statements in the loop body © 2000 Mc. Graw-Hill Introduction to Object-Oriented Programming with Java--Wu 20
- Slides: 20