Difference Between » Break and Continue statement in C

Break and Continue statement in C

A break statement and a continue statement, both are used in C language to change the functioning of a loop to get the desired result.

C language is one of the most widely used languages in computers. While writing a program. We write statements about the function of the program and how it is going to run. We write step by step the process of the program, so that it runs and gives us the desired result. A break statement or continuous statement, both are kind of modifying the written program. To continue a function of a program, we need to control it according to our desire. As each program is specifically designed to perform a specific task, we need to control and alter that program as per our needs. 

Background

In C language, loop is used to accomplish the blocks of code sometimes as per the instructions provided in the loop. A break statement is written for the immediate exit from the most interior loop. Break statement is used in a loop when the case has been executed and we want egress from that loop. Break statement is used in switch as well as in loop. Break statement is used to immediately stop and end the switch or the loop on which it is applied. Basically, you can understand the break statement as the break used in vehicles to stop the vehicle. Once a break statement is launched, it ceases the block and gains the control out of that switch or loop for which it was applied. The break statement is preferably called an ‘exit statement’. A break statement helps in continuing the program after shutting down an enclosing loop. So, the continuity of a loop stops right there when we encounter the ‘break’ statement.

Example of a using a break statement in the program- 

/* trim: remove trailing blanks, tabs, newlines */

int trim (char s [])

{

int n;

For (n = strlen(s) -1; n>= θ; n–)

If (s{n}! = ‘‘&& s[n]! = ‘\t’ && s[n]! = ‘\n’)

break;

S[n+1] = ‘\θ’;

return n;

The continue statement starts the next iteration of the enclosing ‘do’, ‘for’ or ‘while’ loops to commence. It does not do the immediate exit from the loop like a break statement, instead it just orders the loop to move to the next iteration. Continue statement only applies on ‘loop’, not on any other kind of switch. In ‘while’ and ‘do’ loops, the continuous statement gains control of the ‘test condition’ of the loop. In the case of the ‘for’ loop, it gains control of the enhancement stage of the loop. The continue statement is basically used when we want to jump over a few statements in the body of the loop, so that it could pass on the control to the next iteration.  All iterations of the loop are performed even after using the ‘continue’ statement. It is also used to skip the statements in the loop. If there is a switch, and inside that switch, a loop is present, after encountering the continue statement within that loop, the iteration will jump to the next loop. 

Example of  using a continue statement in the program-

/* sum up positive elements of an array */

#include

int main()

{

int a [5] = { -1, 2,-3, 4, -5};

int I, sum = θ;

For ( I = θ; I< 5; I++)

{ if (a[I] < θ) /* skip negative elements */

Continue;

Sum += a[I]; /* sum positive elements */

}

Printf (“Sum of positive elements: %d\n”, sum);

}

OUTPUT

======

Sum of positive elements: 6.

Conclusion

Hopefully, this article would have been helpful in understanding the basic concept behind using the break and continue statements in a program. To put it in a nutshell, break and continue, both statements are applied in the program to basically remove the flaws of the program, so that it could work efficiently and deliver the desired result in the specific time in which it was supposed to complete. We need to demonstrate the program and make it understand what result we want. Break and continue statements are the ways to do our task efficiently. We elaborate the functions in the program for a specific target. By using break and continue statements in a program, we manage the working process of the program.

faq

Frequently asked questions

Get answers to the most common queries related to the General Examination Preparation.

What is the break statement in C language?

Answer- A break statement is the statement written in C language to exit the innermost enclosing loop.

What is the continue statement?

Answer- A continue statement is the statement which is written to jump to the next iteration of the loop....Read full

Is the break statement used in a loop only?

Answer- A break statement is used in both ‘switch’ as well as in ‘loop’, while the continue statement is use...Read full

Are the break and continue statements used in C language only?

Answer- No, break and continue statements can be used in many computer languages like Java, C, C++ etc. Basically, w...Read full

Which statement is used in the C language program for immediate exit from the loop?

Answer- The break statement is used in the program for immediate exit from the innermost loop. We apply this stateme...Read full