Break statement
The break statement terminates execution of the most recent enclosing loop or its conditional statement. Control is passed to the statement (if any) after the end of the statement.
Note
The break statement is used in conjunction with the switch conditional statement and do, for, and while loop statements.
In a switch statement, the break statement causes the program to execute the next statement outside of the switch statement. If there is no break statement, each statement, including the default clause, is executed from the matching case label to the end of the switch statement.
In a loop, the break statement terminates execution of the most recent do, a for, or a while enclosing statement. Control is passed to the statement after the termination statement, if any.
In a nested statement, the break statement terminates only the do, for, switch, or while statements that surround it directly. You can use return or GOTO statements to transfer control from a deeper nested structure.
Example
The following code shows how to use the break statement in a for loop.
#include <iostream>
using namespace std;
int main ()
{
//An example of a standard to loop for
(int i = 1; i < i++)
{
cout << i &L t;< ' \ n ';
if (i = = 4) break
;
An example of a range-based for loop
int nums []{1, 2, 3, 4, 5, 6, 7, 8, 9, ten};
for (int i:nums) {
if (i = = 4) {break
;
}
cout << i << ' \ n ';
}
}
In each use case:
The following code demonstrates how to use a break in a while loop and a Do loop.
#include <iostream>
using namespace std;
int main () {
int i = 0;
while (I <) {
if (i = = 4) {break
;
}
cout << i << ' \ n ';
i++;
}
i = 0;
Do {
if (i = = 4) {break
;
}
cout << i << ' \ n ';
i++;
} while (I <);
}
In each use case:
The following code shows how to use a break in a switch statement. If you are dealing with each use case separately, you must use break in each use case, and if you do not use a break, execute the code in the next use case.
#include <iostream> using namespace std;
Enum suit{Diamonds, Hearts, Clubs, spades};
int main () {Suit hand; ...//Assume that some the enum value is set for hand//In this example, which is handled separately switch (Han
d) {case Diamonds:cout << "got diamonds \ n";
Break
Case Hearts:cout << "got hearts \ n";
Break
Case Clubs:cout << "got clubs \ n";
Break
Case Spades:cout << "got spades \ n";
Break
Default:cout << "didn ' t get card \ n"; }//In this example, diamonds and hearts are handled one way, and//clubs, spades, and the default value are
Another way switch (hand) {case Diamonds:case hearts:cout << ' got a red card \ n ';
Break
Case Clubs:case Spades:default:cout << "didn" t get a red card \ n "; }
}
Continue statement
a force transfer controls the control expression for the least closed do, for, or while loop.
Grammar
Note
All remaining statements in the current iteration will not be executed. Determine the next iteration of the loop, as follows:
In a do or a while loop, the next iteration first recalculates the control expression of a do or a while statement.
In the For loop (using syntax for (init-expr; cond-expr; loop-expr), the LOOP-EXPR clause is executed. Then, recalculate the COND-EXPR clause and determine whether the loop ends or another iteration based on the result.
The following example shows how to use the Continue statement to skip the Code section and start the next iteration of the loop.
Continue_statement.cpp
#include <stdio.h>
int main ()
{
int i = 0;
Do
{
i++;
printf_s ("before proceeding \ n");
Continue;
printf ("After continuing, not output \ n");
} while (I < 3);
printf_s ("After Do loop \ n");
}
Output:
Before proceeding before continuing before the Do loop before continuing