Extra 5% OFF Use Code: OL05
Free Shipping over ₹999

Break & Continue

Break

In C++, the break statement is a control flow statement that is primarily used within loops (such as for, while, and do-while loops) and switch statements.

When a break statement is encountered within a loop or a switch statement, it causes the immediate termination of the innermost enclosing loop or switch statement. After the break statement is executed, the program continues execution from the statement immediately following the terminated loop or switch block.

Example:

#include <iostream>
using namespace std;

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

Output:

0
1
2
3
4

Continue

In C++, the continue statement is a control flow statement that is primarily used within loops to skip the rest of the current iteration and proceed to the next iteration. It is typically used in conjunction with conditional statements to selectively skip certain iterations based on specific conditions.

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

    Leave a Reply

    Your email address will not be published.

    Need Help?