200 likes | 390 Views
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. The while Statement. while ( number <= 100 ) { sum = sum + number; number = number + 1; }.
E N D
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. The while Statement Introduction to Object-Oriented Programming with Java--Wu
while ( number <= 100 ) { sum = sum + number; number = number + 1; } Boolean Expression Statement(loop body) Syntax for the while Statement while ( <boolean expression> ) { <statement> } Introduction to Object-Oriented Programming with Java--Wu
int sum = 0, number = 1 true number <= 100 ? false sum = sum + number; number = number + 1; Control Flow of while Introduction to Object-Oriented Programming with Java--Wu
Example Programs Puuuurginooo … out to reality … While10000.java Puuuurginooo … out to reality … WhileInput.java Introduction to Object-Oriented Programming with Java--Wu
int count = 1; while ( count != 10 ) { count = count + 2; } int product = 0; while ( product < 500000 ) { product = product * 5; } 1 2 while Loop Pitfall - 1 Infinite Loops Both loops will not terminate because the boolean expressions will never become false. Introduction to Object-Oriented Programming with Java--Wu
float count = 0.0f; while ( count <= 1.0f ) { count = count + 0.3333333f; } float count = 0.0f; while ( count != 1.0f ) { count = count + 0.3333333f; } 1 2 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. Introduction to Object-Oriented Programming with Java--Wu
count = 1; while (count < 10) { . . . count++; } count = 0; while (count <= 10) { . . . count++; } count = 1; while (count <= 10) { . . . count++; } count = 0; while (count < 10) { . . . count++; } 1 3 2 4 and exhibit off-by-one error. 1 3 while Loop Pitfall - 3 • Goal: Execute the loop body 10 times. Introduction to Object-Oriented Programming with Java--Wu
Example Programs Grunk … out to reality … WhileLog.java Grunk … out to reality … WhileBoolean.java Introduction to Object-Oriented Programming with Java--Wu
int sum = 0, number = 1; do { sum += number; number++; } while ( sum <= 1000000 ); These statements are executed as long as sum is less than or equal to 1,000,000. The do-while Statement Introduction to Object-Oriented Programming with Java--Wu
Statement(loop body) Boolean Expression Syntax for the do-while Statement do { <statement> } while (<boolean expression>); do { sum += number; number++; } while (sum <= 1000000); Introduction to Object-Oriented Programming with Java--Wu
int sum = 0, number = 1 sum += number; number++; true sum <= 1000000 ? false Control Flow of do-while Introduction to Object-Oriented Programming with Java--Wu
Example Programs Orque … out to reality … DoWhileInput.java Orque … out to reality … DoWhileDrink.java Introduction to Object-Oriented Programming with Java--Wu
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 Introduction to Object-Oriented Programming with Java--Wu
Checklist for Repetition Control • Watch out for the off-by-one error (OBOE). • Make sure the loop body contains a statement that will eventually cause the loop to terminate. • Make sure the loop repeats exactly the correct number of times. Introduction to Object-Oriented Programming with Java--Wu
int i, sum = 0, number; for (i = 0; i < 20; i++) { number = inputBox.getInteger(); sum += number; } These statements are executed for 20 times ( i = 0, 1, 2, … , 19). The for Statement Introduction to Object-Oriented Programming with Java--Wu
for ( i = 0 ; i < 20 ; i++ ) { number = inputBox.getInteger(); sum += number; } Boolean Expression Initialization Update Statement(loop body) Syntax for the for Statement for ( <initialization>; <boolean expression>; <update> ) <statement> Introduction to Object-Oriented Programming with Java--Wu
i = 0; i ++; false i < 20 ? number = inputBox.getInteger( );sum += number; Control Flow of for true Introduction to Object-Oriented Programming with Java--Wu
Example Programs Poodyplat … out to reality … ForPrint.java Poodyplat … out to reality … ForPower.java Poodyplat … out to reality … ForFibonacci.java Introduction to Object-Oriented Programming with Java--Wu
The Nested-for Statement • Nesting a for loop inside another is a common technique • Generate the following table using nested-for statements. • ForTable.java Introduction to Object-Oriented Programming with Java--Wu
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 Introduction to Object-Oriented Programming with Java--Wu