Break and Continue in C

Break and Continue statements are also known as loop control statement. In this c language tutorial, you will learn the use of break and continue to control loop with the help of example.

Break in C Language.

Suppose you have given the assignment to find a specific number among 1000 numbers. When you find that number, you want to exit from the loop, instead of continuing till the end. For this purpose, you have to use the ‘break’ keyword in your code. break keyword is used with loops and switch case only. whenever a break is encounter in code, it immediate exit from that block. The break is not used with if condition, it must be part of a loop or switch case statement.

Example 1: Example of break statement in C language.

#include<stdio.h>
int main()
{
int n=0;
	while(n<=10)
	{
		n++;
		if(n==4)
			break;
		printf("%d\t",n);
	}
return 0;
}
Sample Output:
1 2 3

Above program will print only 1, 2, 3, because the ‘break‘ statement is encounter at the position of 4, loops stop working, and exit from loop block.


Continue Statement in C.

The continue statement in C is used within loops as a statement. Whenever ‘continue’ encounter in a loop, control moves to the beginning of the loop, skip the current iteration and start next iteration. The below example of ‘continue’ keyword in c, explain everything.

Example 2: Example of continue statement in C programming.

#include<stdio.h>
int main()
{
int n=0;
	while(n<=10)
	{
		n++;
		if(n==4)
			continue;
		printf("%d\t",n);
	}
return 0;
}
Sample Output:
1 2 3 5 6 7 8 9 10 11

Above program will print 1 to 11, but skip 4, because of the ‘continue’ statement is encounter at the position of 4, and control moves to the beginning of the loop.