120 likes | 131 Views
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.
E N D
Chapter 4 Repetition Structures 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
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
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
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
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
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
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
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
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
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
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