6.10 break and continue Statements

In addition to selection and iteration 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 iteration statement.

6.10.1 break Statement

The break statement, when executed in a while, for, dowhile, switch, or foreach, causes immediate exit from the loop or switch. Execution continues with the first statement after the control statement. Figure 6.13 demonstrates a break statement exiting a for. When the if nested at line 13 in the for statement (lines 11–19) determines that count is 5, the break statement at line 15 executes. This terminates the for statement, and the app proceeds to line 21 (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.

Fig. 6.13 break statement exiting a for statement.

Alternate View

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

1 2 3 4
Broke out of loop at count = 5

6.10.2 continue Statement

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

Figure 6.14 uses the continue statement in a for to skip the statement at line 16 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).

Software Engineering Observation 6.1

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.2

There’s 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: First, make your code simple and correct; then make it fast, but only if necessary.

 

Fig. 6.14 continue statement skipping an iteration of a for statement.

Alternate View

  1    // Fig. 6.14: ContinueTest.cs
  2    // continue statement skipping an iteration of a for statement.
  3    using System;
  4
  5    class ContinueTest
  6    {
  7       static void Main()
  8       {
  9          for (int count = 1; count <= 10; ++count) // loop 10 times
 10          {
 11             if (count == 5) // if count is 5,
 12             {
 13                continue; // skip remaining code in loop
 14             }
 15
 16             Console.Write($"{count} ");
 17          }
 18
 19          Console.WriteLine("
Used continue to skip displaying 5");
 20      }
 21    }

1 2 3 4 6 7 8 9 10
Used continue to skip displaying 5
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset