Tutorial on C++ programming break statements and continue statements

  • 2020-05-07 20:07:21
  • OfStack

break statement
The
break statement terminates the most recent closed loop or its conditional statement. Control is passed to the statement after the statement ends, if any.


break;

note
The break statement is used in conjunction with the switch conditional statement and the do, for, and while loops.
In the switch statement, the break statement causes the program to execute the next 1 statement other than the switch statement. If there is no break statement, every statement from the matched case tag to the end of the switch statement, including the default clause, is executed.
In the loop, the break statement terminates the execution of the most recent do, for, or while closed statement. Control is passed to the statement following the termination statement, if any.
In nested statements, the break statement terminates only the do, for, switch, or while statements that directly surround it. You can use return or goto statements to transfer control from deeply nested structures.
The sample
The following code demonstrates how to use the break statement in the for loop.


#include <iostream>
using namespace std;

int main()
{
  // An example of a standard for loop
  for (int i = 1; i < 10; i++)
  {
    cout << i << '\n';
    if (i == 4)
      break;
  }

  // An example of a range-based for loop
int nums []{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

  for (int i : nums) {
    if (i == 4) {
      break;
    }
    cout << i << '\n';
  }
}

In each use case:


1
2
3

The following code shows how to use break in the while loop and do loop.


#include <iostream>
using namespace std;

int main() {
  int i = 0;

  while (i < 10) {
    if (i == 4) {
      break;
    }
    cout << i << '\n';
    i++;
  }

  i = 0;
  do {
    if (i == 4) {
      break;
    }
    cout << i << '\n';
    i++;
  } while (i < 10);
}

In each use case:


0
1
2
3

The following code demonstrates how to use break in an switch statement. If you want to work with each use case separately, you must use break in each use case. If break is not used, the code in the next 1 use case is executed.


#include <iostream>
using namespace std;

enum Suit{ Diamonds, Hearts, Clubs, Spades };

int main() {

  Suit hand;
  . . .
  // Assume that some enum value is set for hand
  // In this example, each case is handled separately
  switch (hand)
  {
  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 handled 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
forces the transfer of control over the control expressions of the least closed do, for, or while loops.
grammar


continue;

note
All remaining statements in the current iteration will not be executed. Determine the next iteration of the loop, as shown below:
In an do or while loop, the next iteration first recalculates the control expression for the do or while statements.
In the for loop (using the syntax for(init-expr; cond - expr; loop-expr)), which will execute the es95-expr clause. Then, recalculate the cond-expr clause and determine whether the loop ends or another iteration based on the results.
The following example demonstrates how to skip the code section using the continue statement and start the next iteration of the loop.


// continue_statement.cpp
#include <stdio.h>
int main()
{
  int i = 0;
  do
  {
    i++;
    printf_s(" Before we go on \n");
    continue;
    printf(" After continuing, it is not output \n");
   } while (i < 3);

   printf_s(" in do After the cycle \n");
}

Output:


 Before we go on 
 Before we go on 
 Before we go on 
 in do After the cycle 


Related articles: