C++ Loop control statements

In C++, loop control statements break and continue are used to alter the normal flow of the loop.

Break statement:

The break statement is used to terminate the loop and transfer control to the statement following the loop.

Syntax:

while(condition) {
   // statements
   if(condition) {
      break;
   }
}

Example:

#include <iostream>
using namespace std;

int main() {
    int i;

    for(i = 1; i <= 10; i++) {
        if(i == 6) {
            break;
        }
        cout << i << endl;
    }

    return 0;
}

Output:

1
2
3
4
5

Continue statement:

The continue statement is used to skip the current iteration of the loop and continue with the next iteration.

Syntax:

while(condition) {
   // statements
   if(condition) {
      continue;
   }
   // statements
}

Example:

#include <iostream>
using namespace std;

int main() {
    int i;

    for(i = 1; i <= 10; i++) {
        if(i % 2 == 0) {
            continue;
        }
        cout << i << endl;
    }

    return 0;
}

Output:

1
3
5
7
9