1 / 20

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. The while Statement. while ( number <= 100 ) { sum = sum + number; number = number + 1; }.

garnet
Download Presentation

The while Statement

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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

  3. 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

  4. Example Programs Puuuurginooo … out to reality … While10000.java Puuuurginooo … out to reality … WhileInput.java Introduction to Object-Oriented Programming with Java--Wu

  5. 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

  6. 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

  7. 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

  8. Example Programs Grunk … out to reality … WhileLog.java Grunk … out to reality … WhileBoolean.java Introduction to Object-Oriented Programming with Java--Wu

  9. 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

  10. 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

  11. 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

  12. Example Programs Orque … out to reality … DoWhileInput.java Orque … out to reality … DoWhileDrink.java Introduction to Object-Oriented Programming with Java--Wu

  13. 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

  14. 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

  15. 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

  16. 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

  17. i = 0; i ++; false i < 20 ? number = inputBox.getInteger( );sum += number; Control Flow of for true Introduction to Object-Oriented Programming with Java--Wu

  18. 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

  19. 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

  20. 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

More Related