Utilizor
Contact Us
HomeC TutorialC Break/Continue

CC Break/Continue

Controlling loop execution.

C Break and Continue

You have already seen the break statement used in an earlier chapter of this tutorial. It was used to "jump out" of a switch statement.

The break statement can also be used to jump out of a loop.

The continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in the loop.

Examples

Break Example

c example

Stops the loop when i is 4.

#include <stdio.h>

int main() {
  int i;
  
  for (i = 0; i < 10; i++) {
    if (i == 4) {
      break;
    }
    printf("%d\n", i);
  }
  
  return 0;
}

Continue Example

c example

Skips the value 4.

#include <stdio.h>

int main() {
  int i;
  
  for (i = 0; i < 10; i++) {
    if (i == 4) {
      continue;
    }
    printf("%d\n", i);
  }
  
  return 0;
}

Break in While Loop

c example

Using break in a while loop.

#include <stdio.h>

int main() {
  int i = 0;
  
  while (i < 10) {
    if (i == 4) {
      break;
    }
    printf("%d\n", i);
    i++;
  }
  
  return 0;
}

Test Your Knowledge

1. Which statement stops the loop completely?

2. Which statement skips the current iteration?