C++ break and continue


In C++, the break and continue statements are used to control the flow of loops (like for, while, and do-while) and switch statements. They provide a way to alter the normal flow of control in a loop or switch-case structure.

break Statement

The break statement is used to exit a loop or switch statement prematurely. When a break statement is encountered, the control is immediately transferred to the statement following the loop or switch.

Usage in Loops

When used in loops, break will terminate the current loop iteration and exit the loop entirely.

Example: Using break in a Loop

#include <iostream> int main() { for (int i = 1; i <= 10; i++) { if (i == 5) { break; // Exit the loop when i is 5 } std::cout << "Current number: " << i << std::endl; } std::cout << "Exited the loop." << std::endl; return 0; }

Output:

Current number: 1 Current number: 2 Current number: 3 Current number: 4 Exited the loop.
  • In this example, the loop prints numbers from 1 to 10, but when i reaches 5, the break statement is executed, causing the loop to exit before printing 5 or any subsequent numbers.

Usage in Switch Statement

In switch statements, break prevents the fall-through behavior, where control passes to the next case without explicitly being told to do so.

Example: Using break in a Switch Statement

#include <iostream> int main() { int option = 2; switch (option) { case 1: std::cout << "Option 1 selected." << std::endl; break; // Prevent fall-through case 2: std::cout << "Option 2 selected." << std::endl; break; // Prevent fall-through case 3: std::cout << "Option 3 selected." << std::endl; break; // Prevent fall-through default: std::cout << "Invalid option." << std::endl; } return 0; }

Output:

Option 2 selected.
  • In this example, if we did not include break after each case, the program would execute the statements of subsequent cases until it encounters a break or reaches the end of the switch.

continue Statement

The continue statement is used to skip the current iteration of a loop and move to the next iteration. When continue is encountered, the control jumps to the next iteration of the loop, skipping any remaining statements in the current iteration.

Usage in Loops

When used in loops, continue can conditionally skip the rest of the loop body.

Example: Using continue in a Loop

#include <iostream> int main() { for (int i = 1; i <= 10; i++) { if (i % 2 == 0) { continue; // Skip even numbers } std::cout << "Current number: " << i << std::endl; } return 0; }

Output:

Current number: 1 Current number: 3 Current number: 5 Current number: 7 Current number: 9
  • In this example, the loop prints only odd numbers from 1 to 10. When i is even, the continue statement is executed, skipping the std::cout statement for that iteration.

Summary

  • break Statement:

    • Exits the current loop or switch statement immediately.
    • In loops, it stops the loop completely.
    • In switch statements, it prevents fall-through to subsequent cases.
  • continue Statement:

    • Skips the remaining statements in the current iteration and jumps to the next iteration of the loop.
    • It is often used when you want to skip certain conditions but continue iterating through the rest.

Important Points

  1. Scope: Both break and continue only affect the innermost loop or switch they are in. If you have nested loops, break will exit only the loop where it is called.

  2. Use Cases:

    • Use break when you want to exit a loop based on a specific condition.
    • Use continue when you want to skip an iteration based on a specific condition but continue iterating through the loop.

These statements are essential for controlling the flow of your program in C++ and can make your code cleaner and more efficient by handling conditions effectively.