break and continue Statements

In addition to selection and repetition statements, Java provides statements break and continue (presented in this section and Appendix K, Labeled break and continue Statements) to alter the flow of control. The preceding section showed how break can be used to terminate a switch statement's execution. This section discusses how to use break in a repetition statement.

In addition to the break and continue statements discussed in this section, Java provides the labeled break and continue statements for use in cases in which a programmer needs to conveniently alter the flow of control in nested control statements. We discuss the labeled break and continue statements in Appendix K.

break Statement

The break statement, when executed in a while, for, do...while or switch, causes immediate exit from that statement. Execution continues with the first statement after the control statement. Common uses of the break statement are to escape early from a loop or to skip the remainder of a switch (as in Fig. 5.9). Figure 5.12 demonstrates a break statement exiting a for.

Figure 5.12. break statement exiting a for statement.

(This item is displayed on page 201 in the print version)

 1 // Fig. 5.12: BreakTest.java
 2 // break statement exiting a for statement.
 3 public class BreakTest
 4 {
 5 public static void main( String args[] )
 6 {
 7 int count; // control variable also used after loop terminates
 8
 9 for ( count = 1; count <= 10; count++ ) // loop 10 times
10 {
11 if ( count == 5 ) // if count is 5,
12 break; // terminate loop
13
14 System.out.printf( "%d ", count );
15 } // end for
16
17 System.out.printf( "
Broke out of loop at count = %d
", count );
18 } // end main
19 } // end class BreakTest
 
1 2 3 4
Broke out of loop at count = 5
 

When the if nested at line 11 in the for statement (lines 915) detects that count is 5, the break statement at line 12 executes. This terminates the for statement, and the program proceeds to line 17 (immediately after the for statement), which displays a message indicating the value of the control variable when the loop terminated. The loop fully executes its body only four times instead of 10.

continue Statement

The continue statement, when executed in a while, for or do...while, skips the remaining statements in the loop body and proceeds with the next iteration of the loop. In while and do...while statements, the program evaluates the loop-continuation test immediately after the continue statement executes. In a for statement, the increment expression executes, then the program evaluates the loop-continuation test.

Figure 5.13 uses the continue statement in a for to skip the statement at line 12 when the nested if (line 9) determines that the value of count is 5. When the continue statement executes, program control continues with the increment of the control variable in the for statement (line 7).

Figure 5.13. continue statement terminating an iteration of a for statement.

(This item is displayed on pages 201 - 202 in the print version)

 1 // Fig. 5.13: ContinueTest.java
 2 // continue statement terminating an iteration of a for statement.
 3 public class ContinueTest
 4 {
 5 public static void main( String args[] )
 6 {
 7 for ( int count = 1; count <= 10; count++ ) // loop 10 times
 8 {
 9 if ( count == 5 ) // if count is 5,
10 continue; // skip remaining code in loop
11
12 System.out.printf( "%d ", count );
13 } // end for
14
15 System.out.println( "
Used continue to skip printing 5" );
16 } // end main
17 } // end class ContinueTest
 
1 2 3 4 6 7 8 9 10
Used continue to skip printing 5
 

In Section 5.3, we stated that while could be used in most cases in place of for. The one exception occurs when the increment expression in the while follows a continue statement. In this case, the increment does not execute before the program evaluates the repetition- continuation condition, so the while does not execute in the same manner as the for.

Software Engineering Observation 5.3

Some programmers feel that break and continue violate structured programming. Since the same effects are achievable with structured programming techniques, these programmers do not use break or continue.

Software Engineering Observation 5.4

There is a tension between achieving quality software engineering and achieving the best-performing software. Often, one of these goals is achieved at the expense of the other. For all but the most performance-intensive situations, apply the following rule of thumb: First, make your code simple and correct; then make it fast and small, but only if necessary.


Introduction to Computers, the Internet and the World Wide Web

Introduction to Java Applications

Introduction to Classes and Objects

Control Statements: Part I

Control Statements: Part 2

Methods: A Deeper Look

Arrays

Classes and Objects: A Deeper Look

Object-Oriented Programming: Inheritance

Object-Oriented Programming: Polymorphism

GUI Components: Part 1

Graphics and Java 2D™

Exception Handling

Files and Streams

Recursion

Searching and Sorting

Data Structures

Generics

Collections

Introduction to Java Applets

Multimedia: Applets and Applications

GUI Components: Part 2

Multithreading

Networking

Accessing Databases with JDBC

Servlets

JavaServer Pages (JSP)

Formatted Output

Strings, Characters and Regular Expressions

Appendix A. Operator Precedence Chart

Appendix B. ASCII Character Set

Appendix C. Keywords and Reserved Words

Appendix D. Primitive Types

Appendix E. (On CD) Number Systems

Appendix F. (On CD) Unicode®

Appendix G. Using the Java API Documentation

Appendix H. (On CD) Creating Documentation with javadoc

Appendix I. (On CD) Bit Manipulation

Appendix J. (On CD) ATM Case Study Code

Appendix K. (On CD) Labeled break and continue Statements

Appendix L. (On CD) UML 2: Additional Diagram Types

Appendix M. (On CD) Design Patterns

Appendix N. Using the Debugger

Inside Back Cover



Java(c) How to Program
Java How to Program (6th Edition) (How to Program (Deitel))
ISBN: 0131483986
EAN: 2147483647
Year: 2003
Pages: 615

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net