break vs continue in C++

Jinku Hu Oct 12, 2023
  1. Use the break Statement Operator to Terminate the Loop Body
  2. Use the continue Statement to Skip Portion of Loop Body
break vs continue in C++

This article will demonstrate multiple methods about how to use break vs continue statements in C++.

Use the break Statement Operator to Terminate the Loop Body

The break statement similar to continue is called the jump statement, which is used to interrupt the flow of program execution. In this case, break is utilized to terminate the for loop statement. Note that, when the break is reached and executed, the program leaves the loop body and continues from the next statement - cout << item << "3". break must be used in conjunction with iteration or switch statement and it only affects the nearest enclosing loop/switch.

#include <iostream>
#include <vector>

using std::cin;
using std::cout;
using std::endl;
using std::for_each;
using std::string;
using std::vector;

int main() {
  vector<string> arr1 = {"Gull", "Hawk"};

  for (auto &item : arr1) {
    cout << item << " 1 " << endl;
    for (const auto &item1 : arr1) {
      cout << item << " 2 " << endl;
      if (item == "Hawk") {
        break;
      }
    }
    cout << item << " 3 " << endl;
  }

  return EXIT_SUCCESS;
}

Output:

Gull 1
Gull 2
Gull 2
Gull 3
Hawk 1
Hawk 2
Hawk 3

Use the continue Statement to Skip Portion of Loop Body

The continue statement is the language feature that can be utilized to terminate the current loop iteration and begin executing the next iteration. continue can only be used in for, while or do while loops. If the statement is put inside multiple nested loop blocks, continue will interrupt only the inner loop block iteration and move on to evaluating the condition expression.

In the following example, the continue statement is reached if the current vector element equals Hawk. Once it’s executed, the program evaluates the for loop expression, currently if there are any other elements left in the vector. If true, cout << item << " 2 " line is executed, otherwise cout << item << " 3 " is reached.

#include <iostream>
#include <vector>

using std::cin;
using std::cout;
using std::endl;
using std::for_each;
using std::string;
using std::vector;

int main() {
  vector<string> arr1 = {"Gull", "Hawk"};

  for (auto &item : arr1) {
    cout << item << " 1 " << endl;
    for (const auto &item1 : arr1) {
      cout << item << " 2 " << endl;
      if (item == "Hawk") {
        continue;
      }
    }
    cout << item << " 3 " << endl;
  }
  cout << endl;

  return EXIT_SUCCESS;
}

Output:

Gull 1
Gull 2
Gull 2
Gull 3
Hawk 1
Hawk 2
Hawk 2
Hawk 3
Author: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn Facebook

Related Article - C++ Loop