Break and Continue

C has "continue" and "break" keywords to either continue or exit from the loop. However "break" is also used with "switch" but here one thing to remember is that switch block is not a loop thus continue is not used with switch.

Continue is used to skip current block of statements for one iteration and to jump to the beginning of the loop for condition checking so that it can begin next iteration. Break however is to exit from the loop and directly go to the end of the loop. Here we have three diagrams for each type of loops to explain the control flow of "continue" and "break" in for, while and do-while loops.

For loop - Break/Continue

for (i  = 0; i < MAX; i++) {
    if (condition) {
      continue ; break ;/*Break or continue here */
   }
}

For loop with continue and break

While loop - Break/Continue

while (condition) {
    if (condition) {
      continue ; break ;/*Break or continue here */
   }
}

While loop with continue and break

Do While loop - Break/Continue

do {
    if (condition) {
      continue ; break ;/*Break or continue here */
   }
} while (condition);

Do-While loop with continue and break

Here we want to mention one point is both continue and break works on the current/inner loop thus it will not work for the outer loop. This is a scenario when we have two or more nested loops and breaking from the inner loop then control will likely go to the end of inner loop only. It will not break from outer loops.

for (= 0; j < MAX_J; j++) /* Outer Loop */
{
  for (= 0; I < MAX_I; i++) /*Inner Loop */
  {
    /*Break or continue here */
  }
}

About our authors: Team EQA

You have viewed 1 page out of 248. Your C learning is 0.00% complete. Login to check your learning progress.

#