Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C# break Statement

C# break Statement

C# Loop C# Loops

In C#, break statement has the following two uses:

  1. When break When a statement appears inside a loop, the loop terminates immediately, and program flow continues with the next statement immediately following the loop.

  2. It can be used to terminate a switch case within a statement.

If you are using nested loops (i.e., one loop inside another), the break statement stops executing the innermost loop and then begins executing the next line of code after that block.

Syntax

In C#, break statement syntax:



break;

Flowchart

Example

Example

using

System

;

namespace

Loops

{

   

   

class

Program

   

{

       

static

void

Main

(

string

[

]

args

)

       

{

           

/* Local variable definition */

           

int

a

=

10

;

           

/* while loop execution */

           

while

(

a

<

20

)

{

Console

.

WriteLine

(

"Value of a: {0}"

, a

)

;

                a

++;

               

if

(

a

15

)

               

{

                   

/* Use the break statement to terminate the loop */

                   

break

;

               

}

           

}

            Console

.

ReadLine

(

)

;

       

}

   

}

}

When the code above is compiled and executed, it produces the following result:



a 的值: 10

a 的值: 11

a 的值: 12

a 的值: 13

a 的值: 14

a 的值: 15

C# Loop C# Loops