The Best Explanation to Break and Continue Statements in C++

Both Break and Continue are known as jump statements. Sometimes you might want to skip statements inside the loop, and sometimes you might want the loop to terminate. In situations like these, break and continue statements come in handy. In this tutorial, you will learn about Break and Continue statements in C++.

What Are Break and Continue Statements in C++?

Break and continue are known as jump statements because they are generally used to change or manipulate the regular flow of the program, loops, etc. when a particular condition is met.

The break statement is used to terminate the execution of the current loop. Whenever there is a need to end the loop, you need to add the break statement. Once the break statement is met, all the iterations are stopped, and the control is shifted outside the loop.

Post Graduate Program: Full Stack Web Development

in Collaboration with Caltech CTMEEnroll Now
Post Graduate Program: Full Stack Web Development

The continue statement is used to move to the next iteration, it is also used for termination, but unlike break, it is not used to terminate the entire execution of the loop but only the current iteration of the loop.

Now you will learn about the working of Break and Continue statements in C++.

How Does the Break Statement Work?

The break statement helps with leaving the loop even before its termination condition is met. The break statement basically stops the execution of the loop. Inside the loop body, there is a condition on which you will set the break statement, or you can say on which you want to break the loop; whenever that condition is met, then the execution of the loop is terminated.

C++Example7.

In case the if condition is not true, then it will move to the next iteration.

Now, understand it with the help of an example.

Break_and_Continue_in_C++_Example2.

Here the loop is iterating from 0 to 10. Inside the loop, there is a condition and then the break statement, ensuring that the loop will terminate when i becomes equal to 4.

After the break statement, the control will jump out of the loop body.

This is the output of the example.

Break_and_Continue_in_C++_Example8.

New Course: Full Stack Development for Beginners

Learn Git Command, Angular, NodeJS, Maven & MoreEnroll Now
New Course: Full Stack Development for Beginners

How Does the Continue Statement Work?

The continue statement helps in skipping a particular iteration of the loop. It doesn’t stop the complete execution of the loop; it just skips that particular iteration and then continues with the next iteration.

Inside the loop, if the condition is met, then it will skip the iteration and then continue with the next one. But if the condition is not true, then it will execute the remaining part of the loop.

Break_and_Continue_in_C++_Example3.

Now, understand the continue statement with the help of an example.

Break_and_Continue_in_C++_Example4.

Here the loop is iterating from 0 to 10.

Inside the loop, there is a condition and then the continue statement that will ensure that if the value of i becomes 4, then terminate that iteration and move to the next iteration.

After that, elements are getting printed with the help of cout function.

This is the output of the example.

Break_and_Continue_in_C++_Example9.

Now, understand with an example in which you will use both break and continue statements.

Break_and_Continue_in_C++_Example5

In this example, there are two kinds of racing that take place. One is a drag race, and another one is a lap race. The condition for a drag race is there must be each and every car present before the race, or the race will not take place, but in a lap race, there is no such condition, if any car is not present in lap race we can skip that car.

Inside the drag race, there is a condition saying that car number 4 is not there, so a break statement is used inside that condition because the race will not proceed. So, there is no point in counting the cars anymore.

Inside the lap race, there is the same condition, but in this case, you used the continue statement because you know the race will still take place even if some cars don't participate. So continue will skip that car number and continue with the next.

This is the output.

Break_and_Continue_in_C++_Example6.

Now, understand the difference between Break and Continue statements in C++.

Full Stack Web Developer Course

To become an expert in MEAN StackView Course
Full Stack Web Developer Course

Break Statement vs. Continue Statement

Break

Continue

Break statement stops the entire process of the loop.

Continue statement only stops the current iteration of the loop.

Break also terminates the remaining iterations.

Continue doesn’t terminate the next iterations; it resumes with the successive iterations.

Break statement can be used with switch statements and with loops

Continue statement can be used with loops but not switch statements.

In the break statement, the control exits from the loop.

In the continue statement, the control remains within the loop.

It is used to stop the execution of the loop at a specific condition.

It is used to skip a particular iteration of the loop.

Advance your career as a MEAN stack developer with the Full Stack Web Developer - MEAN Stack Master's Program. Enroll now!

Conclusion

After reading this tutorial on Break and Continue statements in C++, you would have understood the basics of Break and Continue statements. You learned about the working of break and continue statements in C++ with the help of some examples. You also looked at the difference between Break and Continue statements.

If you are looking to build a career in software development, then you can check the Post-Graduate Program in Full Stack Development by Simplilearn. It can prove to be the perfect solution to help you build your career towards the right direction.

Do you have any questions regarding this ‘Break and Continue statements in C++’ tutorial? If you do, then you can put them in the comments section. We’ll help you solve your queries. To learn more about Break and Continue statements in C++, click on the following link: Break and Continue statements in C++.

About the Author

Harsh BhardwajHarsh Bhardwaj

Harsh Bhardwaj is currently working as a Research analyst at Simplilearn digital content marketing team. He holds a bachelor’s degree in Computer Science Engineering. He is proficient in C++, Java, CSS, HTML, Bootstrap, and query language like SQL and worked on web development framework like React.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.