Curriculum
Course: C basic
Login

Curriculum

C basic

C Introduction

0/1

C Get Started

0/1

C Comments

0/1

C Constants

0/1

C Operators

0/1

C Break and Continue

0/1

C User Input

0/1

C Memory Address

0/1

C Structures

0/1
Text lesson

C Break / Continue

Break

You’ve encountered the break statement previously in this tutorial, where it was employed to exit a switch statement prematurely.

The break statement can also facilitate exiting a loop.

In this example, the for loop terminates when i equals 4:

Example

int i;

for (i = 0; i < 10; i++) {
  if (i == 4) {
    break;
  }
  printf(“%d\n”, i);
}

Continue

The continue statement interrupts the current iteration within the loop when a specified condition is met, proceeding directly to the next iteration.

In this example, the value 4 is skipped:

Example

int i;

for (i = 0; i < 10; i++) {
  if (i == 4) {
    continue;
  }
  printf(“%d\n”, i);

Break and Continue in While Loop

Break and continue can also be utilized within while loops:

Break Example

int i = 0;

while (i < 10) {
  if (i == 4) {
    break;
  }
  printf(“%d\n”, i);
  i++;

Continue Example

int i = 0;

while (i < 10) {
  if (i == 4) {
    i++;
    continue;
  }
  printf(“%d\n”, i);
  i++;