Skip to main content

13. Break and Continue Statements

 

Break statement in C

The break is a keyword in C which is used to bring the program control out of the loop. The break statement is used inside loops or switch statement. The break statement breaks the loop one by one, i.e. in the case of nested loops, it breaks the inner loop first and then proceeds to outer loops.

C break statement with the nested loop

#include <stdio.h>

int main()
{
   int i = 1, j = 1;
   for (i = 1; i <= 3; i++){
      for(j = 1; j <= 3; j++){
         printf("%d %d \n", i, j);
         if (i == 2 && j == 2){
            break;
         }
      }
   }
   return 0;
}

1 1
1 2
1 3
2 1
2 2
3 1
3 2
3 3

As we can see the output on the console, 2 3 is not printed because there is a break statement after printing i == 2 and j == 2. But 3 1, 3 2, and 3 3 are printed because the break statement is used to break the inner loop only.

break statement with while loop

#include <stdio.h>

int main()
{
   int a = 0;
   while (1){
      printf("%d ", a);
      a++;
      if (a == 10)
      break;
   }
   printf("\nCame out of while loop.");
   return 0;
}

0 1 2 3 4 5 6 7 8 9
Came out of while loop.

break statement with do-while loop

#include <stdio.h>

int main()
{
   int n = 2, b, choice;
   do{
      b = 1;
      while (b <= 10){
         printf("%d x %d = %d\n", n, b, n*b);
         b++;
      }
      printf("If you want to continue with the table of %d, enter any non-zero number to continue. ", n+1);
      scanf("%d", &choice);
      if (choice == 0){
         break;
      }
      n++;
   }while(1);
   return 0;
}

2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20
If you want to continue with the table of 3, enter any non-zero number to continue. 8
3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
3 x 4 = 12
3 x 5 = 15
3 x 6 = 18
3 x 7 = 21
3 x 8 = 24
3 x 9 = 27
3 x 10 = 30
If you want to continue with the table of 4, enter any non-zero number to continue. 0

Continue statement in C

The continue statement in C language is used to bring the program control to the beginning of the loop. The continue statement skips some lines of code inside the loop and continues with the next iteration. It is mainly used for a condition so that we can skip some code for a particular condition.

#include <stdio.h>

int main()
{
   int i = 0;
   while (i != 10){
      printf("%d \n", i);
      continue;
      i++;
   }
   return 0;
}

Infinite Loop

#include <stdio.h>

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

1
2
3
4
6
7
8
9
10

As we can see, 5 is not printed on the console because loop is continued at i = 5.

#include <stdio.h>

int main()
{
   int i =1, j =1;
   for (i = 1; i <= 3 ; i++){
      for (j =1; j >= 3; j++){
         if (i == 2 && j == 2){
            continue;
         }
         printf("%d %d\n", i, j);
      }
   }
   return 0;
}

1 1
1 2
1 3
2 1
2 3
3 1
3 2
3 3

As we can see, 2 2 is not printed on the console because the inner loop is continued at i == 2 and j == 2.

Comments

Post a Comment