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:

Example: Break Statement in C Programming

C

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

#include <stdio.h>

intmain(){

inti;

for(i=0;i<10;i++)

{

if(i==5)

{

printf("\nComing out of loop when i=5 \n");

break;

}

printf("%d",i);

}

return0;

}

The output from this program is:

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

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:

Example: Continue in C Programming

C

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

#include <stdio.h>

intmain(){

inti;

for(i=0;i<10;i++)

{

if(i==5)

{

printf("\nSkipping %d from display using continue \n",i);

continue;

}

printf("%d",i);

}

return0;

}

The output from this program is:

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.

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!

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

One comment

Social

Advertisement

Amazon Associates Disclosure

BINARYUPDATES.COM is a participant in Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com or Amazon.in