Download presentation
Presentation is loading. Please wait.
1
Repetition Structures
Chapter 4 Repetition Structures Chapter 4 - Repetition Structures
2
Chapter 4 - Repetition Structures
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
Chapter 4 - Repetition Structures
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
Chapter 4 - Repetition Structures
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
Chapter 4 - Repetition Structures
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
Chapter 4 - Repetition Structures
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 continue Statements 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
Chapter 4 - Repetition Structures
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
Chapter 4 - Repetition Structures
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
Chapter 4 - Repetition Structures
Formatted Output (3) The structure of a format descriptor is: Example: Fmt.printf("Average price = $ %5.2f \n", ave); Result: Average price = $ Chapter 4 - Repetition Structures
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.