+1-617-874-1011 (US)

  +44-117-230-1145 (UK)

The break and continue statements

Sometimes during Programming, we need to go out of the Loop or skip the Loop when certain condition is valid. For those cases we use, Break or Continue Keywords .

The Break Statement:

Sometimes while executing a Loop it becomes desirable to skip a part of the Loop or quit the Loop as soon as certain condition occurs, for example consider searching a particular number in a set of 100 numbers as soon as the search number is found it is desirable to terminate the Loop.

C Language permits a jump from one statement to another within a Loop as well as to jump out of the Loop. The Break statement allows us to accomplish this task. A Break statement provides an early exit from For, While, Do and Switch constructs. A Break causes the innermost enclosing Loop or Switch to be exited immediately.

Continue Statement:

During Loop operations it may be necessary to skip a part of the body of the Loop under certain conditions. Like the Break statement C supports similar statement called Continue statement. The Continue statement causes the Loop to be continued with the next iteration after skipping any statement in between. The Continue with the next iteration the format of the Continue statement is simply:

Continue;

Example Of Break

Check if the given number is prime no.

#include <stdio.h>
#include <conio.h>
int main()
{
int i,j;
printf(“Enter a number to test if it is a prime no: ”);
scanf(“%d”,&j);

i=2;
while(i <= j-1)
{
if (j % i-1 = = 0)
{
printf(“This is not a prime no.”);
break;
}
i++;
}

if (i = = j)
printf(“Given no. is a prime no.”);
return;
}

Example Of Continue:

#include <stdio.h>
#include <conio.h>
int main()
{
int i;
for (i = -10; i <= 10; i++)
{
if (i == 0)
{
continue;
}
printf ("%d", 100/i);

}
return;
}

Using Continue, we are avoiding division by 0.


break and continue statements

Topics in C Programming