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 the C language 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 the C language break statement syntax:



break;

Flowchart

Example

Example

#include

 

int

main

(

)

{

   

/* Local variable definition */

   

int

a

=

10

;

   

/* while loop execution */

   

while

(

a

<

20

)

{

printf

(

"Value of a: %d\n"

, a

)

;

      a

++

;

     

if

(

a

15

)

     

{

         

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

         

break

;

     

}

   

}

 

   

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