C++ break and continue Statements

In this lesson, you will learn about the break and continue Statements in C++, their usages, along with examples to better understand them.

C++ Break Statement

In the previous lesson,’ C++ switch Statement,’ you have seen how the ‘break’ statement was used to exist a ‘switch‘ statement. Besides, the ‘break‘ statement is used to exit a ‘for‘ loop, ‘while‘ loop, and ‘do-while‘ loop.

How does ‘break’ work?

The break statement tells the compiler to exist a while loop, do-while loop, for loop, or a switch statement. For example, if you are looping through a customer list and found the customer you are looking for, you can simply exit the loop.

break a while loop

break statement in while loop

Example

#include <iostream>

using namespace std;
int main() {
  int num = 0;
  while (num < 5) {
    cout << num << endl;
    if (num == 3) {
      break;
    }
    num++;
  }
  return 0;
}

Output

1
2
3

break a do-while loop

break statement in do-while loop

Example

#include <iostream>

using namespace std;
int main() {
  int num = 0;
  do {
    cout << num << endl;
    num++;
    if (num == 3) {
      break;
    }
  }
  while (num < 5);
  return 0;
}

Output

0
1
2

break a ‘for’ loop

break a ‘for’ loop

Example

#include <iostream>

using namespace std;
int main() {
  for (int i = 0; i <= 5; i++) {
    cout << i << endl;
    if (i == 3) {
      break;
    }
  }
  return 0;
}

Output

0
1
2
3

C++ Continue Statement

While the break statement exits a loop entirely, the continue statement exit the current iteration and execute the next iteration of a while, do-while, or ‘for’ loop.

How does ‘continue’ work?

The ‘continue‘ statement tells the compiler to exit a current iteration in a while loop, do-while loop, for loop, then move to the next iteration.

‘continue’ in a while loop

continue statement in while loop

Example

#include <iostream>

using namespace std;
int main() {
  int num = 0;
  while (num < 5) {
    if (num == 3) {
      num++;
      continue;
    }
    cout << num << endl;
    num++;
  }
  return 0;
}

Output

0
1
2
4

‘continue’ in a do-while loop

continue statement in do-while loop

Example

#include <iostream>

using namespace std;
int main() {
  int num = 0;

  do {
    num++;
    if (num == 3) {
      continue;
    }
    cout << num << endl;
  }
  while (num < 5);
  return 0;
}

Output

1
2
4
5

‘continue’ in a ‘for’ loop

continue in a ‘for’ loop

Example

#include <iostream>

using namespace std;
int main() {
  for (int i = 0; i <= 5; i++) {

    if (i == 3) {
      continue;
    }
    cout << i << endl;
  }
  return 0;
}

Output

0
1
2
4
5

Points to remember

  • The ‘break’ statement is used to break out a while, do-while, ‘for’ loop, and a switch statement.
  • The ‘continue’ statement is used to exit a current iteration in a while, do-while, and a ‘for’ loop.
Back to: Learn C++ Programming > Control Statements in C++

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.