## 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

``` 1  for (i  = 0; i < MAX; i++) {
2      if (condition) {
3        continue ; break ;/*Break or continue here */
4     }
5  }```

## While loop - Break/Continue

``` 1  while (condition) {
2      if (condition) {
3        continue ; break ;/*Break or continue here */
4     }
5  }```

## Do While loop - Break/Continue

``` 1  do {
2      if (condition) {
3        continue ; break ;/*Break or continue here */
4     }
5  } while (condition);```

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.

``` 1  for (j = 0; j < MAX_J; j++) /* Outer Loop */
2  {
3    for (I = 0; I < MAX_I; i++) /*Inner Loop */
4    {
5      /*Break or continue here */
6    }
7  }```

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

0

Similar topics related to this section