C - Break & continue


1. The break statement is used to terminate a loop or switch statement . It can be used within while , do-while , for , switch statement.

2. A break statement is written simply as
break;

3. If a break statement is included in a while , do-while , for loop , then control will immediately be transferred out of the loop when break statement is encountered.

4. This provides a convenient way to terminate the loop if an error or other irregular condition are detected .

5. In case of nested while , do-while , for or switch statements, the break statement terminates only the immediately enclosing loop or switch,but not the outer ones.

Example of break:

	
#include<stdio.h>
void main(){
	int i;
	for(i=1;i<=10;i++){
		printf("%d\n",i);
		if(i==6)
			break;
	}
	
}
	
	

output:

	
1
2
3
4
5
6
	
	


Example of Continue:

	
#include<stdio.h>
void main(){
	int i;
	for(i=1;i<=10;i++){
		if(i<5)
		continue;
	
		printf("%d\n",i);
	}
	
}
	
	

output:

	
5
6
7
8
9
10
	
	

  • Print Page