break and continue Statements

In addition to the selection and repetition statements, C++ provides statements break and continue 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.

break Statement

The break statement, when executed in a while, for, do...while or switch statement, causes immediate exit from that statement. Program execution continues with the next statement. Common uses of the break statement are to escape early from a loop or to skip the remainder of a switch statement (as in Fig. 5.10). Figure 5.13 demonstrates the break statement (line 14) exiting a for repetition statement.

Figure 5.13. break statement exiting a for statement.

(This item is displayed on pages 209 - 210 in the print version)

 1 // Fig. 5.13: fig05_13.cpp
 2 // break statement exiting a for statement.
 3 #include 
 4 using std::cout;
 5 using std::endl;
 6
 7 int main()
 8 {
 9 int count; // control variable also used after loop terminates
10
11 for ( count = 1; count <= 10; count++ ) // loop 10 times
12 {
13 if ( count == 5 ) 
14  break; // break loop only if x is 5
15
16 cout << count << " ";
17 } // end for
18
19 cout << "
Broke out of loop at count = " << count << endl;
20 return 0; // indicate successful termination
21 } // end main
 
 1 2 3 4
 Broke out of loop at count = 5
 

When the if statement detects that count is 5, the break statement executes. This terminates the for statement, and the program proceeds to line 19 (immediately after the for statement), which displays a message indicating the value of the control variable that terminated the loop. The for statement fully executes its body only four times instead of 10. Note that the control variable count is defined outside the for statement header, so that we can use the control variable both in the body of the loop and after the loop completes its execution.

continue Statement

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

Figure 5.14 uses the continue statement (line 12) in a for statement to skip the output statement (line 14) when the nested if (lines 1112) 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 header (line 9) and loops five more times.

Figure 5.14. continue statement terminating a single iteration of a for statement.

(This item is displayed on pages 210 - 211 in the print version)

 1 // Fig. 5.14: fig05_14.cpp
 2 // continue statement terminating an iteration of a for statement.
 3 #include 
 4 using std::cout;
 5 using std::endl;
 6
 7 int main()
 8 {
 9 for ( int count = 1; count <= 10; count++ ) // loop 10 times
10 {
11 if ( count == 5 ) // if count is 5,
12 continue;  // skip remaining code in loop
13
14 cout << count << " ";
15 } // end for
16
17 cout << "
Used continue to skip printing 5" << endl;
18 return 0; // indicate successful termination
19 } // end main
 
 1 2 3 4 6 7 8 9 10
 Used continue to skip printing 5
 

In Section 5.3, we stated that the while statement could be used in most cases to represent the for statement. The one exception occurs when the increment expression in the while statement follows the continue statement. In this case, the increment does not execute before the program tests the loop-continuation condition, and the while does not execute in the same manner as the for.

Good Programming Practice 5.12

Some programmers feel that break and continue violate structured programming. The effects of these statements can be achieved by structured programming techniques we soon will learn, so these programmers do not use break and continue. Most programmers consider the use of break in switch statements acceptable.

Performance Tip 5.5

The break and continue statements, when used properly, perform faster than do the corresponding structured techniques.

Software Engineering Observation 5.2

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 World Wide Web

Introduction to C++ Programming

Introduction to Classes and Objects

Control Statements: Part 1

Control Statements: Part 2

Functions and an Introduction to Recursion

Arrays and Vectors

Pointers and Pointer-Based Strings

Classes: A Deeper Look, Part 1

Classes: A Deeper Look, Part 2

Operator Overloading; String and Array Objects

Object-Oriented Programming: Inheritance

Object-Oriented Programming: Polymorphism

Templates

Stream Input/Output

Exception Handling

File Processing

Class string and String Stream Processing

Web Programming

Searching and Sorting

Data Structures

Bits, Characters, C-Strings and structs

Standard Template Library (STL)

Other Topics

Appendix A. Operator Precedence and Associativity Chart

Appendix B. ASCII Character Set

Appendix C. Fundamental Types

Appendix D. Number Systems

Appendix E. C Legacy Code Topics

Appendix F. Preprocessor

Appendix G. ATM Case Study Code

Appendix H. UML 2: Additional Diagram Types

Appendix I. C++ Internet and Web Resources

Appendix J. Introduction to XHTML

Appendix K. XHTML Special Characters

Appendix L. Using the Visual Studio .NET Debugger

Appendix M. Using the GNU C++ Debugger

Bibliography



C++ How to Program
C++ How to Program (5th Edition)
ISBN: 0131857576
EAN: 2147483647
Year: 2004
Pages: 627

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