Break Statement:

The break statement is a control inflow statement which is used to terminate the prosecution of a loop if the provided condition is true. Most commonly, break statement is used with loops and switch statements. When the break statement is encountered inside a loop, the program immediately exits that block, and the control moves out of the loop.


break.cpp
#include<iostream>
using namespace std;

int main(){
    for (int i = 1; i < 10; i++) {
        if (i == 5) {
            break; // Exit the loop when i is 5
        }
        cout << i << " "; // Output: 1 2 3 4
    }
}

Output:

As we can see in the code that the counter variable i is initialized with 1. and the for loop will continue until the value of i is less than 10. But when we move inside the loop, we can see that when the value of i is 5, a break statement is encountered which causes the loop to stop and the control will move out of the loop. So the output will be 1 2 3 4.

Continue Statement:

The continue statement is a control inflow statement which is used to skip the current iteration of the loop and continue to the next iteration if the provided condition is true. When the continue statement is encountered in a loop, it jumps to the next iteration, ignoring the code within the loop for the current iteration.


continue.cpp
#include<iostream>
using namespace std;

int main(){
    for (int i = 1; i <= 5; i++) {
        if (i == 3) {
            continue; // skip the iteration when i = 3
        }
        cout << i << " "; // Output: 1 2 4 5
    }
}

Output:

As we can see from the given code that i is initialized with 1 and goes upto 5. But when we move inside the loop, we can see that when the value of iis equal to 3, a continue statement is encountered. When a continue statement is encountered in a loop, it skips that iteration. So when i is equal to 3, that iteration is skipped. So that output will be 1 2 4 5.