Break and Continue Statement in C

Hello all, I hope till now you must be impressed with the power of loops in C programming. Well today I will tell you about the two keywords which are used very frequently inside loops.

These two keywords are given below.

1. break
2. continue

Lets learn about them one by one.

Break Statement in C

It is the keyword which is generally used to break the execution of loop. Sometimes we want to terminate the loop after getting the desired results. In those conditions, break keyword serves our purpose. After encountering the break keyword, control of program shifts to the next statement after the loop. They are used very frequently in the odd loops.

Lets take one example for better explanation.

#include<stdio.h>

void main()
{
 int i;
 
 for(i=0;i<5;i++)
 {
  printf("%d",i);
  break;
  printf("This will not appear!");
 }

 printf("nOnly this will appear!");
}

Output

Break Statement in C

Explanation

  • In the above program I have started a for loop. Inside the body of the loop I have written few statements. First of all the printf() function is executed which will display the first loop counter value.
  • After that I have written break keyword. And just after that I have written another printf() function.
  • But during the execution of program complier will not execute the second printf() function below break keyword. As I said earlier it will take the control of the program outside the loop.
  • And in the last I have another printf() function which is outside the loop. This is executed because it lies outside the loop.

Continue Statement in C

It is a counter part of break keyword. Sometime while writing complex program we need a keyword which will take the control to the beginning. Continue keyword serves this purpose and it is used with loop. So whenever compiler encounters the continue keyword it takes the control to the starting of loop.

A program for continue keyword is given below.

#include<stdio.h>

void main()
{
 int i;

  for(i=0;i<5;i++)
 {
  printf("%dn",i);
  continue;
  printf("This will not appear!");
 }

  printf("nOnly this will appear!");
}



Output

Continue Statement in C

Explanation

  • I have just replaced the break keyword with continue. And it makes the big difference to the program.
  • Execution of the program is similar to the earlier one. Except one thing that this time the loop will be executed fully. But it will not execute the printf() function after continue keyword.
  • Because after the continue keyword the control moves to the beginning. So it will never reach to that function.
  • In the end of the loop last printf() function will get executed.

Try This


You can try your hands on these two keywords by making simple programs.

  • Write a program to check a prime number which is entered by user.
  • Write a program to check composite number which is entered by user.

1 thought on “Break and Continue Statement in C”

  1. why to use continue? even if we dont use it we will get the same output right?
    give me the difference to write continue with different example to clear my doubt

Leave a Comment

Your email address will not be published. Required fields are marked *