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 252. Your C learning is 0.00% complete. Login to check your learning progress.

Further readings

How to write repetitive statements in C? [for, while, do while example, flowchart]
Repetitive tasks in programming. Repetitive statements and loop statements. How to write loop statements. For while do-while loop, syntax and example code.

Write C programming syntax for, while, do while loops.
How to write loop statements in C? How to write a for loop, while loop, do while loop? Study the SYNTAX, FLOW DIAGRAM, with CODE, VIDEO examples.

How to convert for a loop to while loop, vice versa?[GUIDE]
Convert for loop to while loop and vice versa. Understand for, while, do-while loops and write code properly. Also understand on how to convert for, while, do-while to other loops

When is do-while loop preferred over while loop, Give example?
Understand importance and need of do-while loop. How preferred over while loop, Use of do while over while loop. Give example do while vs while usecase

Write infinite loop statements for 'while', 'do-while' and 'for'?
What is an infinite loop in programming? Need of infinite loop. Write infinite loop statements in C. Example of infinite loop 'for' 'while', 'do-while'.

#