Break and Continue Statements

Virtual Lecture

Introduction

C#'s selection and iteration statements are called flow-of-control statements because they provide a highly structured method of conditionally and repeatedly executing a particular section of code.

In days past, however, before the control structures we use today, programmers still needed the equivalent of if statements and loops. Rather than relying on built-in language features, though, those iron-age programmers constructed their own flow of control statements, using the jump .

When it comes to loop control, C# provides two jump statements, break and continue, which let you do your jumping in a controlled fashion.

The Break Statement

The break statement is used in two different circumstances, but both circumstances are quite similar. 

The break statement allows you to jump out of :

When a break statement is encountered in the body of a loop, your program jumps to the statement immediately following the loop body, as you can see in the illustration shown here. Any remaining statements within the loop body are skipped. The test expression is not re-evaluated; the termination occurs regardless of the value the test expression would have.

Although it can be misused, allowing you to write code that is as convoluted as any produced by the goto statement, the break can sometimes be used to make your loops simpler and clearer.

Here's an example, that shows how the break statement can be used to simulate a sentinel loop that stops when the number 50 has been reached. Note that the loop bounds does not need to test for the sentinel value.

Using the break Statement

int num;
int i;
Random rnd = new Random();
for (i = 0; i < 100; i++)
{
     num = rnd.Next(1, 100);
    
if(num == 50)
          break;
}
if (i < 100)
     lblOutput.Text =
"The number 50 was encountered\n";
else
   
 lblOutput.Text = "The number 50 was not encountered\n"
;

The continue Statement

The continue statement is a jump statement like break , but unlike break , the continue jumps back to the loop condition.
Exactly what that means depends upon the loop you're using:

The code below asks the user to input characters from the keyboard. If a digit is encountered, it gets converted and added to the sum. If the character entered is not a digit then the continue statement takes the flow of control back to the top of the loop.

Using continue with a for Loop

char c;
int sum = 0;
for(int i = 0; i < 10; i++)
{
    
Console.WriteLine("Enter and integer to sum");
     c =
Console.ReadKey().KeyChar;
    
Console.ReadLine();
    
if(!char.IsDigit(c))
        
continue;
     sum += (
int)c - '0';
}
Console.WriteLine("The sum of the digits entered is: " + sum);

Code Review

The for loop in the above code will execute 10 times. Each time a character is entered at the keyboard, it is checked to see if it is a digit. The check uses the standard C# method, char.IsDigit(), which returns true if the character entered is between '0' and '9'. Remember that everything entered into the keyboard is a character, which is different from a number.

9 and '9' are different

The char.IsDigit() returns a boolean true if the character is a digit, false otherwise.

If the character entered is a digit then it gets converted to a number by subtracting the character '0' from the character entered. The character '0' has an ascii value of 48. So the above code is equivalent to subtracting 48 from the character entered. In any case, subtracting 48 from the digit will give us the actual binary value of the character representation.

Once the character is converted, it gets added to the sum of the values.

Nested Loops

One of the problems with nested loops, is that the break and continue statements stop "working." Actually, they don't stop working, exactly; they just don't work quite the way you'd like. Here's the problem.

Both break and continue work on the current loop. That means, when you are in an inner loop, a break statement only exits the inner loop, it doesn't get you out of the outer loop.

If you need to break out of both inner and outer loops, then you will have to write some logic to handle it.