Difference Between Break and Continue Statement

Difference Between Break and Continue Statement

5 mins read6.9K Views Comment
Anshuman
Anshuman Singh
Senior Executive - Content
Updated on Mar 27, 2024 11:45 IST

The main difference between Break and Continue statement in C is that the Break statement stops the entire loop process. Whereas the Continue statement only stops the current iteration of the loop.

 

Before we start with the difference between Break and Continue statement, let’s first get a rough idea about these statements. Both of these statements: “Break” and “Continue,” are ‘jump’ statements that transfer control of the program to another part of the program. However, even though both of these statements have a lot of differences between them that sets them apart.

The break statement is primarily used as an exit statement, allowing you to exit the current block or loop. Conversely, the continue statement aids in moving from the current loop iteration to the next loop.

You can also explore: C Online Courses & Certifications

In this article, we will explore the difference between Break and Continue statement in detail. But, before we do so, let’s quickly explore the list of topics listed under the table of contents (TOC) we will cover in this blog.

You can also explore: If-else Statement in C

Table of content

Recommended online courses

Best-suited C / C++ courses for you

Learn C / C++ with these high-rated online courses

4 K
2 months
– / –
6 months
– / –
1 month
3.5 K
3 months
15 K
2 months
– / –
50 hours
– / –
40 hours
– / –
2 months
– / –
4 months
5.5 K
140 hours

Difference between Break and Continue Statement 

For a simpler and better understanding, let’s go through the difference between Break and Continue statement in a tabular format:

Parameter Break statement Continue statement
Allows you to exit from an overall loop construct Yes No
Can be used by switch statement Yes No
The control exits immediately from a loop construct Yes No
Causes a loop to termination Yes No
Can be used with “label” Yes No
Syntax break; continue;

What is a Break Statement?

Break statement definition: The Break statement is a statement that terminates the smallest enclosing loop, such as do-while, for loop, etc.

The keyword “break” is used to end the execution of the current loop in which it is used. When the compiler encounters a break statement, control is transferred to the statement following the loop that contains the break statement.

Let’s try to visualize it with the help of a flow chart:

2022_10_1-18-10-22-1.jpg

You can also explore: Understanding Break Statement in C++

Example of Break Statement

Here’s an example of a Break statement


 
#include < stdio.h >
int main() {
int number;
while (1) { // Infinite loop
printf("Enter a number (0 to exit): ");
scanf("%d", &number);
if (number == 0) {
break; // Break the loop if the user enters 0
}
printf("You entered: %d\n", number);
}
printf("Exited the loop because you entered 0.\n");
return 0;
}
Copy code

Output

Enter a number (0 to exit): 1
You entered: 1
Enter a number (0 to exit): 2
You entered: 2
Enter a number (0 to exit): 3
You entered: 3
Enter a number (0 to exit): 0
Exited the loop because you entered 0.

What is the Continue Statement?

Continue statement definition: The Continue statement is a statement that skips the rest of the loop statement and begins the next iteration of the loop to take place.

The keyword “continue” is also used in control statements like for loop, while loop, and do-while loop. When the compiler encounters a continue statement, it skips the rest of the loop’s statements and transfers control to the nearest loop continuation portion of the enclosing loop.

Let’s try to visualize it with the help of a flow chart

2022_10_2-18-10-22.jpg

Example of Continue statement

Here’s an example of a Continue statement


 
#include < stdio.h >
int main() {
int number;
while (1) { // Infinite loop for demonstration
printf("Enter a number (0 to exit): ");
scanf("%d", &number);
if (number == 0) {
break; // Exit the loop if the user enters 0
}
if (number < 0) {
printf("Negative numbers are skipped.\n");
continue; // Skip to the next iteration for negative numbers
}
printf("You entered: %d\n", number);
}
printf("Exited the loop.\n");
return 0;
}
Copy code

Output

Enter a number (0 to exit): 1
You entered: 1
Enter a number (0 to exit): 2
You entered: 2
Enter a number (0 to exit): -1
Negative numbers are skipped.
Enter a number (0 to exit): -3
Negative numbers are skipped.
Enter a number (0 to exit): 0
Exited the loop.

C programming examples 

Operators in C programming: A Complete Tutorial

Control Statements in C | Meaning and Types

Conclusion

Both the break and continue statements are jump statements that transfer control to another section of the program. The break statement allows the control to exit the loop, and the continue statement allows the control to continue to the next iteration of the loop.

You will be able to code more efficiently now that you understand the difference between the Break and Continue statements.

FAQs

What is the main difference between Break and Continue statement?

The main difference between Break and Continue statement is that the Break statement stops the entire loop process. Whereas the Continue statement only stops the current iteration of the loop.

In terms of the difference between Break and Continue statement, what is the Break statement?

In regards to the difference between Break and Continue statement, the Break statement is a statement that terminates the smallest enclosing loop, such as do-while, for loop, etc.

In terms of the difference between Break and Continue statement, what is the Continue statement?

In regards to the difference between Break and Continue statement, the Continue statement is a statement that skips the rest of the loop statement and begins the next iteration of the loop to take place.

What is the difference between Break and Continue statement in Python?

In regards to the difference between Break and Continue statement in Python, the Break statement terminates the entire loop iteration, whereas the Continue statement skips the current iteration.

In Java, what is the difference between Break and Continue statement?

In regards to the difference between Break and Continue statement in Java, the Break statement immediately exits the innermost switch or enclosing loop. In contrast, the Continue statement initiates the next iteration of the while, for, or do loop.

About the Author
author-image
Anshuman Singh
Senior Executive - Content

Anshuman Singh is an accomplished content writer with over three years of experience specializing in cybersecurity, cloud computing, networking, and software testing. Known for his clear, concise, and informative wr... Read Full Bio