View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
C++ Tutorial

Explore C++ Tutorials: Explori…

  • 76 Lessons
  • 15 Hours

Differences Between Break and Continue

Updated on 03/02/2025484 Views

I've been programming with C++ for several years now, and I've learned that understanding how to manipulate loops with break and continue is an essential skill.

Loops are instrumental in C++ programming for executing a block of code repeatedly until a certain condition is met. However, there might be situations where you'd want to alter this repetitive flow for more granular control. This is where break and continue statements come into play. These keywords act as control flow modifiers, influencing how loops execute in C++.

Let us explore these concepts in greater detail and understand the fundamental difference between break and continue in C++.

Difference Between break and continue With Examples

First, let me show you a table that summarizes the key difference between break and continue in C++ (applicable for other programming languages such as C as well) and then we will look at some examples.

Here is a table that showcases the difference between break and continue in C++::

Feature

break

continue

Functionality

Terminates the enclosing loop prematurely

Skips remaining statements in the current iteration

Control flow

Jumps out of the loop

Jumps to the beginning of the next iteration

Example of using break statement to understand the difference between break and continue in C++:

Code:

#include <iostream>
int main() {
for (int i = 0; i < 10; i++) {
if (i == 5) {
std::cout << "Found the value! Exiting the loop." << std::endl;
break;
}
std::cout << i << " ";
}
return 0; // Indicates successful program execution
}

Explanation:

The above code iterates from 0 to 9 using a for loop. However, when i reaches 5, the if statement triggers. Inside the if block, the break statement is executed. This immediately halts the loop, even though i is less than 10.

Example of using continue statement to understand the difference between break and continue in C++:

Code:

#include <iostream>
int main() {
for (int i = 0; i < 10; i++) {
if (i % 2 == 0) {
continue;
}
std::cout << i << " ";
}
return 0; // Indicates successful program execution
}

Explanation:

This code iterates from 0 to 9. The if statement checks if i is even (if divisible by 2). If so, the continue statement is executed, skipping the std::cout statement in that iteration. Control jumps back to the beginning of the loop, i is incremented, and the loop continues.

break statement in C++

The break statement serves a straightforward purpose, it terminates the enclosing loop entirely, even if the loop's condition hasn't been satisfied yet. Sometimes, you need to stop a loop mid-execution, even if it hasn't reached its natural end. Control jumps out of the loop, and program execution continues with the line of code following the loop.

Here are the applications of break statement in C++:

  • Finding a specific element in an array: Once the element is found, using break can save unnecessary iterations.
  • Early termination based on user input: If a user enters a specific value to quit, break can gracefully exit the loop.

While working on a data processing project, I had a for loop iterating through a massive array. If a particular value was found, I needed to immediately stop searching and perform a calculation. The break statement was my savior. It allowed me to optimize the process and save valuable computational time.

Now, let us check out a more advanced example of break statement in C++ to further understand the difference between break and continue in C++:

Code:

#include <iostream>
int main() {
int numbers[] = {5, 12, 3, -8, 20, 17};
int search_target = -8;
for (int i = 0; i < sizeof(numbers)/sizeof(numbers[0]); i++) {
if (numbers[i] == search_target) {
std::cout << "Found the target value at index: " << i << std::endl;
break; // Terminate the loop once the target is found
}
}
return 0;
}

Explanation:

In this example, we have an array of numbers. The goal is to find the index of a specific search_target within the array. The for loop iterates through the array, and if the current element matches the search_target, the index is printed, and the break statement immediately exits the loop. This optimization prevents unnecessary iterations once the target value is located.

continue statement in C++

We sometimes might want to skip certain iterations of a loop based on conditions, but still keep the loop running. The continue statement, in contrast to break, doesn't terminate the loop altogether. Instead, it skips the remaining statements in the current iteration and jumps directly to the beginning of the next iteration. The loop's condition is reevaluated, and if it's still true, the loop continues.

Here are the applications of continue statement in C++:

  • Processing only specific elements: If you only need to work with odd numbers in a loop, continue can help you bypass even-numbered iterations.
  • Implementing retry logic: In certain scenarios, you might want to retry an operation if it fails. Continue allows you to keep looping and retrying within the loop's condition.

Imagine you're parsing a text file that has mixed data types. You only want to process the lines containing numbers. Here's how continue can help:

for (each line in the file) {
if (line contains only text) {
continue; // Skip to the next line
}
// Process the numeric data in the line
}

Now, let us check out a more advanced example of continue statement in C++ to further understand the difference between break and continue in C++:

Code:

#include <iostream>
#include <string>
int main() {
std::string sentence = "This sentence has some numbers: 12 and 35.";
for (char c : sentence) {
if (isdigit(c)) { // Check if the character is a digit
continue; // Skip numeric characters
}
std::cout << c; // Print non-numeric characters
}
return 0;
}

Explanation:

In this example, the code iterates through a sentence. The isdigit function is used to identify numeric characters. If a digit is encountered, the continue statement skips that iteration, preventing it from being printed. This effectively extracts and prints only the non-numeric characters from the sentence.

If you wish to master C++, you can enroll in one of upGrad’s software engineering courses.

Mastering break and continue

Let us imagine you're building a game in C++, and you want your player to move through levels, collecting coins along the way. But what if they find a special hidden item? You might want to skip collecting any remaining coins on the level and move them directly to the next stage. That's where break and continue step in, giving you control over how your game logic flows.

We covered the difference between break and continue with examples and why these statements are important in C++ programming. Now, let me share some tips with you.

My Tips Based on My Experience

While break and continue seem simple, I've learned two crucial issues to watch out for:

  • Readability: Excessive use of these statements, especially in nested loops, can make your code harder to understand. If you find yourself relying heavily on them, consider redesigning your loops.
  • Infinite loops: Be careful with continue, especially if your loop's condition relies on something modified within the loop, you could accidentally create an infinite loop. Always double-check your logic.

Wrapping Up

We should use break when we want to completely exit the loop upon a specific condition. And, we should use continue when we want to skip the current iteration and proceed to the next one only if the loop's condition remains true.

By effectively using break and continue, you'll gain precise control over how your C++ loops behave. Now that we have covered the difference between break and continue in C++, we have to remember practice and experimentation are key. The more you use these concepts in different coding scenarios, the more comfortable and proficient you'll become with them.

If you wish to learn programming in C++, you should definitely check out upGrad’s computer science and software engineering programs.

Frequently Asked Questions

1. What is the difference between break and continue?

The main difference between break and continue in C++ is that break exits the innermost enclosing loop completely and continue skips the remaining code in the current iteration of the innermost enclosing loop. Also, control jumps to the statement immediately following the loop when we use break while control jumps to the beginning of the next loop iteration when we use continue.

2. What is the difference between continue and return in C++?

As we already discussed, continue skips the remaining code in the current iteration of a loop and proceeds to the next iteration and this statement is used within loop structures. Meanwhile, return exits a function entirely and optionally returns a value. This statement is used within functions to signal completion and send back results.

3. What is the difference between break and exit in C++?

break exits the nearest enclosing loop within a function and execution continues within the current function. On the other hand, exit terminates the entire program immediately, regardless of where it's used. This can introduce resource leaks or unexpected behavior if not used carefully.

4. What is the difference between break, continue and pass statements?

We already discussed the difference between break and continue in C++ where break exits a loop and continue skips to the next iteration of a loop. Meanwhile, pass acts as a placeholder indicating "do nothing" (in Python) in scenarios where a statement is syntactically required.

5. What are the 3 differences between break and continue?

Here is the difference between break and continue in C++:

  • Action: break exits a loop entirely; continue skips to the next iteration of the loop.
  • Control flow: After a break, the program continues with the line after the loop; after a continue the next iteration of the loop begins (if the condition still holds).
  • Situational use: break is used for unconditional exits, continue is used for conditionally skipping parts of a loop's execution.

6. What is the difference between continue and break in while?

The difference between continue and break in C++ when it comes to while loop is the same as the differences in any loop construct.

image
Join 10M+ Learners & Transform Your Career
Learn on a personalised AI-powered platform that offers best-in-class content, live sessions & mentorship from leading industry experts.
advertise-arrow

Free Courses

Start Learning For Free

Explore Our Free Software Tutorials and Elevate your Career.

upGrad Learner Support

Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)

text

Indian Nationals

1800 210 2020

text

Foreign Nationals

+918045604032

Disclaimer

1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.

2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.