Introduction
Fundamentals
C Flow Control
C Functions
C Arrays
Break and Continue Statements in C
The break and continue statements are used to skip some statements inside the loop or terminate the loop immediately without checking the test expression. In this tutorial, we will learn about break and continue statements in c language with the help of examples.
1. Break Statement in C:
When the break statement is found, the loop ends immediately. The break statement can also be used to jump out of a loop.
Syntax:
break;
Example 1: Break Statement
#include <stdio.h>
int main()
{
//printing number from 1 to 10;
for(int i=1;i<10;i++){
if(i==5){
break;
//loop break
}
printf("%d \n",i);
}
return 0;
}
Output :
1 2 3 4
Example 2: Taking input from the user until he/she enters zero.
#include<stdio.h>
int main(){
int i;
while(1){
printf("Enter a number: ");
scanf("%d", &i);
if(i==0){
break;
}
}
return 0;
}
Output :
Enter a number: 3 Enter a number: 4 Enter a number: 2 Enter a number: 0
2. Continue Statement in C:
The continue statement skips the current loop iteration and continues with the next iteration.
Like a break statement, continue statement is also used with if condition inside the loop to alter the flow of control.
Syntax:
continue;
Example 3: printing sum of odd numbers between 0 and 10.
#include <stdio.h>
int main ()
{
int sum = 0;
for (int i = 0; i < 10; i++)
{
if ( i % 2 == 0 )
continue;
sum = sum + i;
}
printf("sum = %d",sum);
return 0;
}
Output :
sum = 25