22
DecContinue Statement in C: What is Break & Continue Statement in C with Example
Break and Continue Statements in C: An Overview
Learning to code can be a difficult endeavor, especially when it comes to techniques used to control program flow. It is important to know the use of break and continue statements in Learn C for writing efficient and organized code. If you're looking for comprehensive guidance on using these statements, consider exploring C Online Training.
What is the Continue Statement in C?
The continue statement is used to skip the current iteration of any loop and bring the control to the beginning of the iteration. The statements following the continue statement are skipped and the iteration starts again.
Syntax
//loop statements
continue;
//some lines of the code which is to be skipped
Example of Continue Statement in C
#include<stdio.h>
void main ()
{
for (int i = 1; i <= 10; i++) {
if (i == 5) {
continue;
}
printf("%d\n", i);
}
- This C program in the C Compiler iterates 10 times using a for loop.
- The continue statement is encountered when i=5. So it skips the remainder of the loop's body for that iteration.
- As a result, it only prints the numbers 1-4 and 6-10, skipping out 5.
Output
1
2
3
4
6
7
8
9
10
Read More - Top 50 C Interview Questions and Answers
What is the Break Statement in C?
If you remember we already came across the break statement while learning the switch statement. We used to check each case in switch and after finding the correct match used to get out using break.
- It is one of the most used jump statements.
- In C, break is used to prematurely exit from a loop or switch statement, before the block has been fully executed.
- As soon as a break statement is encountered inside a loop, the loop terminates immediately, and control is transferred to the next statement outside the loop.
Syntax
//loop or switch case
break;
Example of Break Statement in C
#include <stdio.h>
int main()
{
for(int i=1; i <=10; i++)
{
if(i == 5)
{
break; // terminates the loop when i is equal to 5
}
printf("%d\n", i);
}
return 0;
}
- The above C code iterates from 1 to 10 using a for loop. It determines whether the value of i inside the loop equals 5.
- It outputs numbers from 1 to 4 (inclusive) as a result
- When i=5, the break statement is executed, which abruptly ends the loop.
`Output
1
2
3
4
Where to use Break Statements in C
We can use break statements in two ways in C:
- Break statement in switch case
- Break statement in a loop
- Break Statement in Switch Case in C
The break statement is used inside a switch statement to terminate the execution of the enclosing switch statement and exit the block of code. When a break statement is encountered inside a switch block, the control flow is transferred to the end of the switch block, and the remaining case statements are skipped.
You have already executed this while learning the switch case in C.
Syntax
switch (expression) {
case 1:
// code to be executed if expression is equal to 1
break;
case 2:
// code to be executed if expression is equal to 2
break;
case 3:
// code to be executed if expression is equal to 3
break;
default:
// code to be executed if expression doesn't match any case
break;
}
- Break statement in a loop in C
We will see here, how to use the break statement in all three types of loops
The break statement can be used anywhere inside the for loop's body. When executed, it immediately terminates the loop, and control is transferred to the statement following the loop.
The example you have executed above for the break statement is an example of a break statement in a for loop.
Example of Break statement in a for loop
#include <stdio.h>
int main()
{
for (int i = 1; i <= 10; i++)
{ if (i == 5)
{ break; // terminates the loop when i is equal to 5
}
printf("%d\n", I);
}return 0;
}
The above C code in the C Editor iterates from 1 to 10 using a for loop. It determines whether the value of i inside the loop equals 5.
It outputs numbers from 1 to 4 (inclusive) as a result
When i=5, the break statement is executed, which abruptly ends the loop.
Output
1
2
3
4
Example of Break statement in a while loop
#include<stdio.h>
void main ()
{
int i = 0;
while(1)
{ printf("%d\n",i);
i++;
if(i == 5)
break;
}
printf("The loop terminates");
}
Here, the loop would have become infinite without the break statement. When i becomes 5, the control comes out of the otherwise infinite while loop.
Output
0
1
2
3
4
The loop terminates
Example of Break statement in a do...while loop
#include<stdio.h>
void main ()
{
int n=1,i,choice;
do
{
i=1;
while(i<=10)
{
printf("%d X %d = %d\n",n,i,n*i);
i++;
}
printf("Do you want to further print the table of %d , enter any non-zero value to continue.",n+1);
scanf("%d",&choice);
if(choice == 0)
{
break;
}
n++;
}while(1);
}
Output
1 X 1 = 1
1 X 2 = 2
1 X 3 = 3
1 X 4 = 4
1 X 5 = 5
1 X 6 = 6
1 X 7 = 7
1 X 8 = 8
1 X 9 = 9
1 X 10 = 10
Do you want to further print the table of 2, enter any non-zero value to continue.2
2 X 1 = 2
2 X 2 = 4
2 X 3 = 6
2 X 4 = 8
2 X 5 = 10
2 X 6 = 12
2 X 7 = 14
2 X 8 = 16
2 X 9 = 18
2 X 10 = 20
Do you want to further print the table of 2, enter any non-zero value to continue.0
Difference between Break and Continue Statements in C
Parameters | Break Statement in C | Continue Statement in C |
Purpose | Exits the current loop | Skips the current iteration |
Applicability | Used within loops (for, while, do-while) | Used within loops (for, while, do-while) |
Effect | Terminates the loop prematurely and continues with the next statement after the loop | Immediately moves to the next iteration of the loop, skipping the remaining code in the current iteration |
Loop Control | Affects the entire loop | Affects only the current iteration of the loop |
Typical Use Cases | When a specific condition is met, and you want to exit the loop | When you want to skip the current iteration of the loop based on a certain condition and continue with the next iteration |
Example | for (int i = 1; i <= 10; i++) { if (i == 5) { break; } printf("%d\n", i); } | for (int i = 1; i <= 10; i++) { if (i == 5) { continue; } printf("%d\n", i); } |
Summary
To summarize, breaks and continues allow for more advanced flexibility in C programming. These break and continue statements in the C example and different types of break and continue statements in the C language allow the programmer to control the content of the loop, which can save a lot of time down the line. Therefore, understanding how these statements work and when to use them is important when developing in C. It's also crucial to explore other online resources, including C Course, to gain a comprehensive understanding of this break and continue statement in C and become a better programmer.