Introduction
When the programmer can construct the loop he needs inside his program, he can build the program he needs to solve problems.
The article C++ break and continue more about loops will discuss how the programmer can control the body of his loop using break and continue statements.
What are loops?
Loops are a programming construct that allows a programmer to execute a block of code repeatedly. A loop is made up of three components: the initializing statement, the condition, and the update statement.
The initializing statement sets up some states that will be used in the loop’s execution. The condition statement specifies when the loop should stop running. The update statement changes some states after each iteration of the loop.
C++ break and continue
Continue statement in C++
The statement “continue” is a computer instruction that tells the program to skip the remainder of the instructions in the current loop iteration and resume with the next iteration.
The “statement” continue in the body of the loop means that:
- No additional statements in the body of the loop are executed over and over.
- The program goes instantly to start the next iteration.
Break statement in C++
A break statement is a statement in the C++ programming language that forces the execution of the loop to terminate.The break statement is used to end a while or do-while loop, or to terminate an iteration in a for loop.The break statement means:
- No additional statements in the body of the loop are executed over and over.
- The loop is definitely terminated.
What is the main difference between the continue and break statement in C++?
The loop construct is the third essential block of code in C++ programming language; therefore, loops need more statements inside the body of the loop to deliver precise control. Continue and break statements are pieces of code inside the body of the C++ loop.
They do the same job related to the control, but the main difference between the continue and the break statement is that the continue statement has to start the next iteration; however, the break statement has to terminate the loop.
How to use continue statements in C++
The code of the program
#include <iostream>#include<vector>using namespace std;int main () {vector<int> Prime_numbers {3,5,7,9,11,13,17,19,23,31};for(int prime: Prime_numbers){if(prime==9)continue;cout<<prime<<endl;}return 0;}
The output of the program
When the program arrives at element 9, start the next iteration.
How to use break statements in C++
The code of the program
#include <iostream>#include<vector>using namespace std;int main () {vector<int> Prime_numbers {3,5,7,9,11,13,17,19,23,31};for(int prime: Prime_numbers){if(prime==9)break;cout<<prime<<endl;}return 0;}
The output of the program
When the program arrives at element 9, the program terminates.
Conclusion
C++ break and continue statements are used to control the flow of execution of a program.The break statement terminates a loop, and the continue statement starts the next iteration of a loop