The break Statement


Syntax

 break;

Notes

The break statement may only occur within one of the following statements:

The purpose of the break statement is to cause the flow of execution to be transferred to the end of the enclosing statement. Upon encountering a break, control is passed to the first statement following the enclosing statement.

The continue statement may also be used to manipulate the flow of control within a loop, but without terminating the loop as does a break statement. A continue statement may only be specified within the body of either a while statement or a for statement.

Examples

The following example adds the numbers 0-4.

int j=0;
for (int i=0; i<10; i++)
 {
  if (i==5) break;
  j += i;
 }
 
 // Loop adds numbers 0 - 4, upon exit i==5, j==10.

The following example also adds the numbers 0-4.

int i=0;
int j=0;
while(i<10)
 {
  if (i==5) break;
  j += i;
  i++;
 }
 
 // Loop adds numbers 0 - 4, upon exit i==5, j==10.