1 / 12

Repetition Structures in Java

Learn about while, do/while, and for loops in Java, as well as the continue and break statements. Explore how to use labeled break/continue statements and formatted output using the Fmt class.

roxanng
Télécharger la présentation

Repetition Structures in Java

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. Chapter 4 Repetition Structures Chapter 4 - Repetition Structures

  2. The while Loop • A while loop contains a statement or block of statements that are repeated indefinitely as long as some boolean expression is true. The test is performed at the beginning of the loop. • Examples: while ( booleanExpr ) while ( booleanExpr ) { statement; statement 1; statement 2; ... } • Be careful not to place a semicolon after the while state-ment–that would produce a null statement as the body of the loop. Chapter 4 - Repetition Structures

  3. The do/while Loop • A do/while loop also contains a statement or block of statements that are repeated indefinitely as long as some boolean expression is true, but the test is performed at the end of the loop. • Every do/while loop is executed at least once. • Example: do { statement 1; statement 2; ... } while ( booleanExpr ) Chapter 4 - Repetition Structures

  4. The for Loop (1) • A for loop executes a block of statements a specified number of times. • The structure of a for loop is: Chapter 4 - Repetition Structures

  5. The for Loop (2) • When a for loop starts, the loop index is set to initExpr, and contineExpr is evaluated. If the continuation expression is true, the loop is executed. • When the loop finishes executing, incrementExpr is executed to change the value of the loop index, and contineExpr is evaluated again. If the continuation expression is still true, the loop is executed again. • This process continues until contineExpr is false. Then execution skips to the first statement after the end of the loop. Chapter 4 - Repetition Structures

  6. The continue Statement • The continue statement interrupts the execution of a loop, and returns control to the top of the loop. • Example: • Result: Chapter 4 - Repetition Structures

  7. The break Statement • The break statement interrupts the execution of a loop, and transfers control to the first statement after the loop. • Example: • Result: Chapter 4 - Repetition Structures

  8. break and continueStatements in Nested Loops • In nested loops, an ordinary break or continue statement applies to the innermost loop only. • Example: • Results: Chapter 4 - Repetition Structures

  9. Labeled break/ continue Statements • A label can be added to each loop in a nested structure, and the label can be used to specify which loop a break or continue statement applies to. • Example: • Results: Chapter 4 - Repetition Structures

  10. Formatted Output • Unlike languages such as C and Fortran, Java has no simple way to format numbers for display • Class chapman.io.Fmt makes up for this lack. It contains two methods: printf, which prints formatted data to the standard output stream, and sprintf, which creates a formatted string. • This class is very useful for creating structured tables of data, or for specifying the number of decimal digits to print out. Chapter 4 - Repetition Structures

  11. Formatted Output (2) • The syntax of method printf is: Fmt.printf(format,value); where format is a format string and value is the value to be printed. • The format string must contain a format descriptor (marked by an initial % character), which serves as a “model” for how to print out the value. • All characters in the format string except for the format descriptor are printed unchanged. Chapter 4 - Repetition Structures

  12. Formatted Output (3) • The structure of a format descriptor is: • Example: Fmt.printf("Average price = $ %5.2f \n", ave); • Result: Average price = $ 3.77 Chapter 4 - Repetition Structures

More Related