Break and Continue in C Programming

Break and Continue in C Programming are control statements. While writing programs often time situation occurs where we want to jump out of loop instantly without waiting to satisfy conditional test. The keyword break allows us to do this. When break is encountered inside any loop, control automatically passes to the first statement after the loop. A break is usually associated with an if. The keyword break, breaks the control only from the loop in which it is placed.

Break Statement

Let’s understand break using C example program:

#include <stdio.h>

int main(){

  int i;

  for(i=0; i<10; i++)
  {
      if(i==5)
      {
          printf("\nComing out of loop when i=5 \n");
          break;
      }

      printf("%d", i);
  }
   return 0;
}

The output from this program is:

Output of Break Statement in C Programming
Output of Break Statement in C Programming

Thus, it is clear that a break statement takes the execution control out of the loop. In above program, if we omitted break statement then what will be output? The answer is:

Output of break in C Programming-II
Output of Break in C Programming-II

Because when i‘s value is 5, condition will satisfy and executed printf statement, after that compiler goes to next statement i.e. printf(“%d”, i); because loop do not terminate and it is run till the value of ‘i’ reaches to 9.

Continue Statement

In C Programs, if we want to take control to the beginning of the loop, by passing the statements inside the loop, which have not yet been executed, in this case we use continue. continue is reserved keyword in C. When continue is encountered inside any loop, control automatically passes to the beginning of loop. A continue is usually associated with if Statement. As shown in the example program:

#include <stdio.h>

int main(){

  int i;

  for(i=0; i<10; i++)
  {
      if(i==5)
      {
          printf("\nSkipping %d from display using continue \n", i);
          continue;
      }

      printf("%d", i);
  }

   return 0;
}

The output from this program is:

Output of Continue in C Programming
Output of Continue in C Programming

When the value of i equals to 5, the continue statement takes the control to if statement by passing the value when i==5 inside for loop. This shows that it is possible to skip the rest of the commands in the current loop and start from the top again.learn-embedded-system

Here is an end of this post, we hope now you’re comfortable to work with Break and Continue in C Programming. These control statements are very useful while writing programs. In our next lesson we’ll learn about Functions in C Programming. Please do write us if you have any suggestion/comment or come across any error on this page. Thanks for reading!

Get Free Courses & Webinars
You'll receive only high quality learning material, tips & tricks
I agree to have my personal information transfered to MailChimp ( more information )
We respect your privacy

About Umesh Lokhande

Umesh Lokhande holds a Master degree in Scientific Instrumentation from University of Applied Sciences Jena, Germany. and has previously worked at Orbotech, Alere Technologies etc. Umesh is also a founder and first author of BINARYUPDATES.COM

Login

Register | Lost your password?