Break and Continue Statement in C are looping control statements. Based on break and continue statement loop can be break or skipped
break and continue both are keywords.
break – Uses to break the loop also use with switch to break the switch case.
continue– skip the current execution of loop and transfer control to beginning of loop
For Loop in C programming
While Statement in C Programming
Do While Statement in C Programming
Break Statement in C
To terminate or end the current loop execution break statement is used.
Example of Break Statement in For Loop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #include<stdio.h> int main() { int i=0; for(int i=1;i<=10;i++){ if(i==5){ break; } printf("\nValue is %d ",i); } printf("\nOutside Loop"); } |
Inside for loop we set some condition inside if statement if condition is true then break statement executes and transfer control to outside loop.
Output
1 2 3 4 5 | Value is 1 Value is 2 Value is 3 Value is 4 Outside Loop |
Example of Break Statement in While Loop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #include<stdio.h> int main() { int i=1; while(i<=10){ if(i==5){ break; } printf("\nValue is %d ",i); i++; } printf("\nOutside Loop"); } |
1 2 3 4 5 | Value is 1 Value is 2 Value is 3 Value is 4 Outside Loop |
Example of Break Statement in do While Loop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #include<stdio.h> int main() { int i=1; do{ if(i==5){ break; } printf("\nValue is %d ",i); i++; }while(i<=10); printf("\nOutside Loop"); } |
1 2 3 4 5 | Value is 1 Value is 2 Value is 3 Value is 4 Outside Loop |
Example of Break Statement in Switch Statement
To terminate switch break statement break is used inside switch case statement
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #include<stdio.h> int main() { int n; printf(" enter number between 1 to 3"); scanf("%d", &n); switch (n) { case 1: printf("\n One"); break; case 2: printf("\n Two"); break; case 3: printf("\n Three"); break; default: printf("\n Wrong Number"); } return 0; } |
1 2 3 | enter number between 1 to 3 2 Two |
Example of Break Statement in Nested Loop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #include<stdio.h> int main() { int i=0,j=0; int a[10]={1,2,3,4,5,6,7,8,9,10}; int b[5]={1,6,9,12,15}; for(i=0;i<10;i++){ for(j=0;j<=5;j++){ if(a[i]==b[j]){ printf("\nNumber found %d",b[j]); break; } } } } |
Here break keyword is used inside two loops to check whether two array element matches or not.
if element found then break is used to break inner loop.
1 2 3 4 | enter number between 1 to 3 Number found 1 Number found 6 Number found 9 |
Example of Break Statement In User Control Loop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | #include<stdio.h> #include<string.h> int main() { int i = -1, j = 0; char name[10][10]; char name1[10]; while (1) { printf("\nEnter names (to end Type Bye): "); scanf("%s", name1); if (strcmp(name1, "Bye") == 0) { //i=i-1; break; } if (i == 10) { printf("\nArray is full"); break; } i++; strcpy(name[i], name1); } for (j = 0; j <= i; j++) { printf("\nname [%d] = %s", j, name[j]); } } |
In this program array name is two dimensional array that stores names.
We get input from user and store in name array.
if array is full or user type Bye then we call break statement to end the loop.
Finally we display all names.
This way an infinite loop can be controlled by user.
1 2 3 4 5 6 7 8 9 10 11 | Enter names (to end Type Bye): Ram Enter names (to end Type Bye): Mohan Enter names (to end Type Bye): Sohan Enter names (to end Type Bye): Bye name [0] = Ram name [1] = Mohan name [2] = Sohan |
Continue Statement in C
Continue is used to skip the current loop execution and transfer control to beginning of loop.
Example of Continue Statement in C For Loop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #include<stdio.h> int main() { int i=0; for(int i=1;i<=6;i++){ if(i==3){ continue; } printf("\nValue is %d ",i); } printf("\nOutside Loop"); } |
loop will start from 1 and end when it is greater than or equals to 6.
inside loop a condition is checking that i==3 if true then continue is statement is get executed which transfers the control to beginning of loop.
1 2 3 4 5 6 | Value is 1 Value is 2 Value is 4 Value is 5 Value is 6 Outside Loop |
Example of Continue Statement in While Loop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #include<stdio.h> int main() { int i = 0; while (i <= 6) { if (i == 3) { i++; continue; } printf("\nValue is %d ", i); i++; } printf("\nOutside Loop"); } |
Example of Continue Statement in Do While Loop
1 2 3 4 5 6 7 8 9 10 11 12 13 | #include<stdio.h> int main() { int i = 0; do { if (i == 3) { i++; continue; } printf("\nValue is %d ", i); i++; } while (i <= 6); printf("\nOutside Loop"); } |
1 2 3 4 5 6 7 | Value is 0 Value is 1 Value is 2 Value is 4 Value is 5 Value is 6 Outside Loop |
What is the use of continue statement in C
Continue is used to skip the current loop iteration and transfer the control to beginning of loop.
What is the use of break statement in C
Break is used to break a inner most loop.
break vs continue in C
break is used to terminate the loop where as continue is used to skip current iteration of loop.
1 2 3 4 5 6 | for(int i=1;i<=10;i++){ if(i==5){ break; } printf("\nValue is %d ",i); } |
here when i=5 then loop will be terminated
1 2 3 4 5 6 | for(int i=1;i<=10;i++){ if(i==5){ continue; } printf("\nValue is %d ",i); } |
when i=5 then loop will not continue current iteration. It will start with other iteration