continue Statement in C++ and break Statement in C++

In C++, break and continue are loop control statements used to alter the flow of loops such as for, while, and do-while.

They help in controlling execution by skipping or stopping iterations based on conditions.

1. break Statement in C++

The break statement is used to terminate the loop immediately when a condition is met.

It exits the loop and transfers control to the next statement after the loop.

C++
break example
#include <iostream>
using namespace std;

int main() {
    for(int i = 1; i <= 5; i++) {
        if(i == 3)
            break;
        cout << i << " ";
    }
    return 0;
}
Output:
1 2

2. continue Statement in C++

The continue statement skips the current iteration and moves to the next iteration of the loop.

It does not terminate the loop, only skips execution for a specific condition.

C++
continue example
#include <iostream>
using namespace std;

int main() {
    for(int i = 1; i <= 5; i++) {
        if(i == 3)
            continue;
        cout << i << " ";
    }
    return 0;
}
Output:
1 2 4 5

3. Difference Between break and continue

break: Terminates the loop completely.

continue: Skips current iteration and continues loop.

4. Flow Understanding

break → exit loop immediately.

continue → skip iteration, continue loop.

5. Real-World Usage

break is used when a condition is satisfied and no further processing is needed. continue is used to skip unwanted data while processing loops.

6. Using in Nested Loops

break affects only the innermost loop. continue applies to the current loop iteration.

7. Common Mistakes

1. Using break instead of continue.

2. Misunderstanding loop flow.

3. Infinite loops due to incorrect logic.

8. Best Practices

1. Use break when loop termination is needed.

2. Use continue for skipping iterations.

3. Avoid excessive use for readability.

4. Write clear conditions.

9. Practice Exercises

1. Print numbers skipping multiples of 3.

2. Stop loop when number equals 5.

3. Use break in nested loops.

Conclusion

The break and continue statements are powerful tools for controlling loop execution in C++.

Understanding their differences helps you write efficient and readable code.