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

C++ break Statement

C++ break Statement

C++ Loops 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

C++ break Statement

Example

Example

#include

using

namespace

std

;

 

int

main

(

)

{

   

// local variable declaration

   

int

a

=

10

;

   

// do loop execution

   

do

   

{

       

cout

<<

"Value of a: "

<<

a

<<

endl

;

       a

=

a

1

;

       

if

(

a

15

)

       

{

         

// terminate the loop

         

break

;

       

}

   

}

while

(

a

<
20
)

;

 

   

return

0

;

}

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++ Loops C++ Loops