C++ Break and Continue C-PLUS-PLUS

C++ Break and Continue  

C++ Break and Continue

C++ Break

You have already seen the break statement used in earlier topics of this tutorial. It was used to "jump out" of a switch statement. The break statement can also be used to jump out of a loop.

Example

#include <iostream>
using namespace std;

int main() {
  for (int i = 0; i < 10; i++) {
    if (i == 3) {
      break;
    }
    cout << i << "\n";
  } 
  return 0;
}

Output:

0
1
2

C++ Continue

The continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in the loop.

This example skips the value of 5:

Example

#include <iostream>
using namespace std;

int main() {
  for (int i = 0; i < 10; i++) {
    if (i == 5) {
      continue;
    }
    cout << i << "\n";
  }   
  return 0;
}

Output:

 

0
1
2
3
4
6
7
8
9

Download free E-book of C-PLUS-PLUS


#askProgrammers
Learn Programming for Free


Join Programmers Community on Telegram


Talk with Experienced Programmers


Just drop a message, we will solve your queries