1 / 21

Flow of Control

Flow of Control. Loops – Chapter 3.2. Java Loop Statements: Outline. the while Statement the do-while Statement the for Statement. Java Loop Statements, cont. A portion of a program that repeats a statement or a group of statements is called a loop.

wilsoni
Download Presentation

Flow of Control

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. Flow of Control Loops – Chapter 3.2

  2. Java Loop Statements: Outline • the while Statement • the do-while Statement • the for Statement

  3. Java Loop Statements, cont. • A portion of a program that repeats a statement or a group of statements is called a loop. • The statement or group of statements to be repeated is called the body of the loop. • A loop could be used to compute grades for each student in a class. • There must be a means of exiting the loop.

  4. the while Statement • also called a while loop • A while statement repeats until a controlling boolean expression becomes false. • If the controlling boolean expression is false initially, the while loop is not executed. • The loop body typically contains a statement that ultimately causes the controlling boolean expression to become false.

  5. the while Statement, cont. • syntax while (Boolean_Expression) Body_Statement or while (Boolean_Expression) { First_Statement Second_Statement … }

  6. Example int count=0; while (count<5) { System.out.println(count); count++ } System.out.println(″count after loop = ″ + count);

  7. The do-while Statement • also called a do-while loop • similar to a while statement, except that the loop body is executed at least once • syntax do Body_Statement while (Boolean_Expression); • don’t forget the semicolon!

  8. The do-while Statement, cont. • First, the loop body is executed. • Then the boolean expression is checked. • As long as it is true, the loop is executed again. • If it is false, the loop is exited. • equivalent while statement Statement(s)_S1 while (Boolean_Condition) Statement(s)_S1

  9. Example int count = 0; do { System.out.println(count) count++; } while (count < 0); System.out.println(″count after loop = ″ + count);

  10. Infinite Loops • A loop which repeats without ever ending is called an infinite loop. • If the controlling boolean expression never becomes false, a while loop or a do-while loop will repeat without ending.

  11. The for Statement • A forstatement executes the body of a loop a fixed number of times. • example for (count = 1; count < 3; count++) System.out.println(count); System.out.println(“Done”);

  12. Exercise • What output is produced by the following code? int nfor (n=1; n<=4; n++)System.out.println(n); int nfor (n=1; n>4; n++)System.out.println(n);

  13. Exercise • What output is produced by the following code? double n; for (n=0; n<3; n = n + 0.5)System.out.println(n); • Write a for statement that writes out even numbers 2,4,6,8,10.

  14. Bewarethe Empty for Statement • What is printed by int product = 1, number; for (number = 1; number <= 10; number++); product = product * number; System.out.println(product);? • The last semicolonproduces an emptyforstatement.

  15. The Beware the Empty while Statement int product = 1, number = 1; while (number <= 10); { product = product * number; number++ } System.out.println(product); • The last semicolon in while (number <= 10); produces an empty while loop body.

  16. Choosing a Loop Statement • If you know how many times the loop will be iterated, use a for loop. • If you don’t know how many times the loop will be iterated, but • it could be zero, use a while loop • it will be at least once, use a do-while loop. • Generally, a while loop is a safe choice.

  17. The break Statement in Loops • A break statement can be used to end a loop immediately. • The break statement ends only the innermost loop or switch statement that contains the break statement. • break statements make loops more difficult to understand. • Use break statements sparingly (if ever).

  18. Loop Bugs • common loop bugs • unintended infinite loops • off-by-one errors • testing equality of floating-point numbers • subtle infinite loops • The loop may terminate for some input values, but not for others. • For example, you can’t get out of debt when the monthly penalty exceeds the monthly payment.

  19. Programming Exercise • Write a program that reads a list of positive numbers terminated by a negative number and then prints the total and average.

More Related