1 / 26

Flow of Control: Loops

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

sinead
Download Presentation

Flow of Control: Loops

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 4

  2. Java Loop Statements: Outline The while statement The do-while statement The for Statement

  3. Java Loop Statements 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. There must be a means of exiting the loop.

  4. The while Statement Also called a while loop A while statement repeats while a controlling boolean expression remains true The loop body typically contains an action that ultimately causes the controlling boolean expression to become false.

  5. The while Statement Listing 4.1class WhileDemo Sample screen output

  6. The while Statement • Figure 4.1 The action of the while loop in Listing 4.1

  7. The while Statement Syntax while (Boolean_Expression) Body_Statement or while (Boolean_Expression) { First_Statement Second_Statement … }

  8. The while Statement • Figure 4.2Semantics of the while statement

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

  10. The do-while Statement 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

  11. The do-while Statement • Figure 4.4 The Semantics of the do-while Statement

  12. The do-while Statement listing 4.2class DoWhileDemo Sample screen output

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

  14. Nested Loops The body of a loop can contain any kind of statements, including another loop.

  15. The for Statement A for statement executes the body of a loop a fixed number of times. Example for (count = 1; count < 3; count++) System.out.println(count);

  16. The for Statement Syntax for (Initialization, Condition, Update) Body_Statement Body_Statementcan be either a simple statement or a compound statement in {}. Corresponding while statement Initialization while (Condition) Body_Statement_Including_Update

  17. The for Statement View sample program, Listing 4.4 class ForDemo Sample screen output

  18. The for Statement • Figure 4.5 The action of the for loop in listing 4.5

  19. The for Statement • Figure 4.6 The semantics of the for statement

  20. The for Statement • Possible to declare variables within a for statement int sum = 0; for (int n = 1 ; n <= 10 ; n++) sum = sum + n * n; • Note that variable n is local to the loop

  21. The for Statement A comma separates multiple initializations Example for (n = 1, product = 1; n <= 10; n++) product = product * n; Only one boolean expression is allowed, but it can consist of &&s, ||s, and !s. Multiple update actions are allowed, too. for (n = 1, product = 1; n <= 10; product = product * n, n++);

  22. Quiz 3 • Write the Java statements that will add the integers 1 to 50, and print out the sum. You must use for statement. You do not need to write a complete program. • Make sure you write your name and your section of 145 on your paper.

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

  24. Programming Tips (demo) • Assignments submission format change • Programming format (indentation, or Ctrl+Shift+F or Ctrl+I in Eclipse) • Comments in javadoc format (see Daniel Pade’s website) • Variable scope: in a block specified by { } or in a method (local variables), in a class (instance variables) • Check available labs and homeworks before coming to the class and think about it, otherwise it’ll be difficult to catch up with the class • e.g. read homework 3 before next class

  25. class demo • I changed the name in the class from ForDemo to MySquare.java • It get the side width of a square and calculate the area • /** • * calculate the area of a square • * @author Hongying • * • */ • public class MySquare { • private int i; • /** • * @return the i • */ • public int getI() { • return i; • } • /** • * @param i • * the i to set • */ • public void setI(int i) { • this.i = i; • }

  26. /** • * • * @param i • */ • public MySquare(int i) { • super(); • this.i = i; • } • /** • * • * @return area of the square • */ • public int area(){ • return i*i; • } • public static void main(String[] args) { • MySquare a = new MySquare(5); • System.out.println(a.area()); • } • }

More Related