Export (0) Print
Expand All

The C break Statement

The break statement terminates the execution of the nearest enclosing do, for, switch, or while statement in which it appears. Control passes to the statement that follows the terminated statement.

Syntax

jump-statement :
break;

The break statement is frequently used to terminate the processing of a particular case within a switch statement. Lack of an enclosing iterative or switch statement generates an error.

Within nested statements, the break statement terminates only the do, for, switch, or while statement that immediately encloses it. You can use a return or goto statement to transfer control elsewhere out of the nested structure.   

This example illustrates the break statement:

#include <stdio.h>

int main(){
   char c;
   for(;;)
   {
      printf("\nPress any key, Q to quit: ");
      scanf("%c", &c);
      if (c == 'Q')
         break;
   }
}   //Loop exits only when 'Q' is pressed

See Also

break (C++ Reference)

Show:
© 2015 Microsoft