Node:Terminating loops with break, Next:, Previous:Terminating and speeding loops, Up:Terminating and speeding loops



Terminating loops with break

The usual statement to terminate a loop is the same statement that is used to jump out of switch statements:

break;

If this statement is encountered within a loop, the loop will end immediately. For instance, here is an inefficient way of assigning 12 to my_int:

for (my_int = 1; my_int <= 100; my_int++)
{
  if (my_int == 12)
  {
    break;
  }
}

printf("my_int = %d\n\n", my_int);