C Sharp/Break and Continue Statement

From Meshplex

Jump to: navigation, search
Image:Csharp_programming.gif

Main Home

Basics
C# Tutorial Home
C# - Introduction to Visual Studio IDE
Introduction to C#
C# - Overview
C# - Statements
C# - Data Types
C# - Variables
C# - Operators
C# - Flow Control

C# - Variables II
C# - Functions and Methods
C# - Classes and Objects I
C# - Enumerations
C# - Dates and Times
C# - Random Numbers

Advanced
C# - Inheritance
C# - Polymorphism
C# - Garbage Collection
C# - Operator Overloading
C# - Encapsulation
C# - Properties
C# - Indexers
C# - Exceptions
C# - GUI
C# - Delegates
C# - Events
C# - Components
C# - Multithreading
C# - Regular Expressions
C# - Graphics and Multimedia
C# - Files and Streams
C# - XML
C# - Database, SQL and ADO.NET
C# - ASP.NET Web Forms and Web Controls
C# - Web Services
C# - Network Programming
C# - Datastructures and Collections
C# - Enumerations and Iterators
C# - .NET Assemblies
C# - CLR
C# - Visual Studio Debugger
C# - Namespaces
C# - Generics
C# - MS Intermediate Language
C# - Deploying Windows Application

[edit] C# Break Statement

The break statement alters the flow of a loop. It immediately exits the loop based upon a certain condition. After the loop is exited it continue to the next block of instructions. Exiting a loop early can boost program performance, it avoids unnecessary loops.

Lets take a look at some real code. The code is suppose to loop ten times but will exit prematurely and only loop five times.

[edit] C# Break Example

using System;
 
class Program
{
    static void Main(string[] args)
    {
        for(int count = 1; count <= 10; count++)
        {
            if (count >= 6)
                break;  //if condition is met
                        //exit the loop
 
            Console.WriteLine(count);
        }
        Console.Read();
    }
}

Output:

1
2
3
4
5


As you can see when the if condition is evaluated to true the break statement is executed. The for loop is prematurely exited immediately and it does not execute any other code in the loop. The break statement also works for while, do/while, and switch statements.

[edit] C# Continue Statement

The continue statement jumps over the following code within a loop. In other words it skips the proceeding code in the loop and continues to the next iteration in the loop. It does not exit the loop like the break will. Just like the break to work properly the continue statement needs an if condition to let the program know that if a certain condition is true the continue statement must be executed.

When the continue and break statements are used together it can greatly increase program performance within the loop structure. In the program example below there is an algorithm that will count from 1 to 10. The program will skip the fifth iteration to prove that the continue statement actually works.


using System;
 
class Program
{
    static void Main(string[] args)
    {
        for(int count = 1; count <= 10; count++)
        {
            if (count == 5)
                continue;   //condition is met
                            //skip the code below
 
            Console.WriteLine(count);
        }
        Console.Read();
    }
}


Output:

1
2
3
4
6
7
8
9
10


The output skipped the number 5. When the condition is true the continue statement is executed and the remaining code is skipped. That means Console.WriteLine(count); command is skipped on the fifth iteration.