The Jump Statements break and continue

   


The Jump Statements break and continue

The following section discusses how the break and continue statements can be used to influence the flow of execution during the execution of a loop statement.

The break Statement

Occasionally programmers want to terminate a loop somewhere inside its loop body rather than at the beginning or at the end of the loop. They can achieve this by using the break statement that was introduced along with the switch statement in the previous chapter.

The break Statement and the Iteration Statements

graphics/common.gif

The break statement can be used to terminate an iteration statement and will, when executed, cause the flow of control to jump out to the next statement immediately following the iteration statement. The break statement can be positioned anywhere inside the loop body to implement this effect.


Syntax Box 9.4 The break Statement

 break_statement::=             break; 

The break statement is applied in line 20 of Listing 9.7 as part of a program that enables the user to transfer individually chosen characters from a given text into another text (see the sample output after Listing 9.7). Initially, the user enters a text. Each individual letter is then displayed one-by-one and for each letter the user can choose to either insert the letter into the new text or skip the letter. This repeated set of actions can be terminated at any time by pressing the letter T, causing the extracted letters to be displayed.

Listing 9.7 TransferLetters.cs
01: using System; 02: 03: class TransferLetters 04: { 05:     public static void Main() 06:     { 07:         string myText; 08:         string answer; 09:         string newText = ""; 10: 11:         Console.WriteLine("Enter some text:"); 12:         myText = Console.ReadLine(); 13:         Console.WriteLine("Type I<enter> to insert, " + 14:             "<enter> to skip and T<enter> to terminate"); 15:         for (int i=0; i<myText.Length; i++) 16:         { 17:             Console.Write(myText[i] + " "); 18:             answer = Console.ReadLine().ToUpper(); 19:             if (answer == "T") 20:                 break; 21:             if (answer == "I") 22:                 newText = newText + myText[i]; 23:         } 24:         Console.WriteLine("The new text is: " + newText); 25:     } 26: } Enter some text: There is one way to find out if a man is honest - ask him. If he says yes you know he is  graphics/ccc.gifcrooked. - Groucho Marx<enter> Type I<enter> to insert, <enter> to skip and T<enter> to terminate T <enter> h <enter> e <enter> r <enter> e <enter>   <enter> i <enter> s <enter>   <enter> o i<enter> n i<enter> e i<enter>    i<enter> w i<enter>  a i<enter> y i<enter>    T <enter> The new text is: one way 

We are faced with two contrasting requirements when choosing an iteration statement for this program.

On one hand, we need an entry condition loop because myText, which is visited character by character, might be empty, in which case we never want to execute the loop body.

On the other hand, we need an exit condition loop because we only know if the user wants to terminate the loop if at least the first character of myText has been presented. This calls for the loop body to be executed at least partially.

There are many ways to solve this computational problem. In this case, I chose to use the for loop to accommodate for the entry condition requirement and the break statement (after all, this listing is supposed to demonstrate the break statement) along with an if statement (see lines 19 and 20) to act instead of an exit condition. The for loop beginning in line 15 ensures that the loop body is repeated at most myText.Length times. If the user enters a t (or T) after one of the individual characters printed on the console by line 17, the Boolean expression of line 19 is evaluated to be true triggering the break statement of line 20 to be executed. The for statement is then terminated and the flow of control moves to line 24.

Note

graphics/common.gif

The break statement is most frequently used together with an if statement, as shown in Listing 9.7, acting as an internal loop condition.


The continue Statement

The continue statement makes the flow of execution skip the rest of a loop body to continue with the next loop. This ability is sometimes useful.

The continue Statement and the Iteration Statements

graphics/common.gif

When the continue statement is executed inside the loop body of an iteration statement, it causes the flow of control to jump over the remaining statements in the loop body and begin at the next iteration. If used with a while or a do-while loop, the loop condition will be the first part of this next iteration to be evaluated. In a for loop, the flow of control is passed to the loop update.


Syntax Box 9.5 The continue Statement

 Continue_statement::=                  continue; 

Note:

  • The continue statement can only be utilized inside an iteration loop.

The continue statement does not interfere with the number of times the loop body is repeated as does the break statement, but merely influences the flow of control in any one particular iteration. Listing 9.8 utilizes the continue statement in line 10 to prevent the program from printing every fourth number.

Listing 9.8 Counting.cs
01: using System; 02: 03: class Counting 04: { 05:     public static void Main() 06:     { 07:         for (int i = 0; i <= 20; i++) 08:         { 09:             if (i % 4 == 0) 10:                 continue; 11:             Console.Write(i + ", "); 12:         } 13:     } 14: } 1, 2, 3, 5, 6, 7, 9, 10, 11, 13, 14, 15, 17, 18, 19, 

i is declared and initialized to zero in line 7. The for loop then increments it by 1 and repeatedly executes the loop body until i is greater than 20. Whenever i is a multiple of 4 (as tested for in line 9), the continue statement is executed and jumps over the remaining part of the loop body (in this case, line 11) and back to the loop update (i++) in line 7 to begin another iteration. As a result all numbers between zero and twenty, except those that are multiples of 4, are printed onscreen.


   


C# Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 286
Authors: Stephen Prata

Similar book on Amazon

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