break and continue Statements

In addition to 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 to terminate any repetition statement.

break Statement

The break statement, when executed in a while, for, do...while, switch, or foreach, 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 repetition statement or to skip the remainder of a switch (as in Fig. 6.9). Figure 6.12 demonstrates a break statement exiting a for.

Figure 6.12. break statement exiting a for statement.

 1 // Fig. 6.12: BreakTest.cs
 2 // break statement exiting a for statement.
 3 using System;
 4
 5 public class BreakTest
 6 {
 7 public static void Main( string[] args )
 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 ) // if count is 5,
14 break; // terminate loop
15
16 Console.Write( "{0} ", count );
17 } // end for
18
19 Console.WriteLine( "
Broke out of loop at count = {0}", count );
20 } // end Main
21 } // end class BreakTest
 
1 2 3 4
Broke out of loop at count = 5

When the if nested at line 13 in the for statement (lines 1117) determines that count is 5, the break statement at line 14 executes. This terminates the for statement, and the application proceeds to line 19 (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 because of the break.

continue Statement

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

Figure 6.13 uses the continue statement in a for to skip the statement at line 14 when the nested if (line 11) 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 9).

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

 1 // Fig. 6.13: ContinueTest.cs
 2 // continue statement terminating an iteration of a for statement.
 3 using System;
 4
 5 public class ContinueTest
 6 {
 7 public static void Main( string[] args )
 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 Console.Write( "{0} ", count );
15 } // end for
16
17 Console.WriteLine( "
Used continue to skip printing 5" );
18 } // end Main
19 } // end class ContinueTest
 
1 2 3 4 6 7 8 9 10
Used continue to skip printing 5

In Section 6.3, we stated that the while statement can 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 application evaluates the repetition-continuation condition, so the while does not execute in the same manner as the for.

Software Engineering Observation 6 3

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

Software Engineering Observation 6 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.


Preface

Index

    Introduction to Computers, the Internet and Visual C#

    Introduction to the Visual C# 2005 Express Edition IDE

    Introduction to C# Applications

    Introduction to Classes and Objects

    Control Statements: Part 1

    Control Statements: Part 2

    Methods: A Deeper Look

    Arrays

    Classes and Objects: A Deeper Look

    Object-Oriented Programming: Inheritance

    Polymorphism, Interfaces & Operator Overloading

    Exception Handling

    Graphical User Interface Concepts: Part 1

    Graphical User Interface Concepts: Part 2

    Multithreading

    Strings, Characters and Regular Expressions

    Graphics and Multimedia

    Files and Streams

    Extensible Markup Language (XML)

    Database, SQL and ADO.NET

    ASP.NET 2.0, Web Forms and Web Controls

    Web Services

    Networking: Streams-Based Sockets and Datagrams

    Searching and Sorting

    Data Structures

    Generics

    Collections

    Appendix A. Operator Precedence Chart

    Appendix B. Number Systems

    Appendix C. Using the Visual Studio 2005 Debugger

    Appendix D. ASCII Character Set

    Appendix E. Unicode®

    Appendix F. Introduction to XHTML: Part 1

    Appendix G. Introduction to XHTML: Part 2

    Appendix H. HTML/XHTML Special Characters

    Appendix I. HTML/XHTML Colors

    Appendix J. ATM Case Study Code

    Appendix K. UML 2: Additional Diagram Types

    Appendix L. Simple Types

    Index



    Visual C# How to Program
    Visual C# 2005 How to Program (2nd Edition)
    ISBN: 0131525239
    EAN: 2147483647
    Year: 2004
    Pages: 600

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