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.
Let’s understand break using C example program:
[crayon-5c452998e8fb8294315081/] The output from this program is:
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:
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.
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:
[crayon-5c452998e8fcb626538530/] The output from this program is:
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!