Break and Continue Statements

In C++, break and continue statements are control flow statements that alter the flow of execution within loops. They are used to control the iteration process and provide more flexibility in loop constructs.

break; statement

The break statement in C++ is a control flow statement that allows you to prematurely exit from a loop. It is commonly used to terminate the loop when a certain condition is met, avoiding unnecessary iterations. Let’s explore the break statement in more detail:

				
					break;

				
			

Usage:

  • The break statement can only be used inside loops, such as for, while, or do-while loops.
  • When the break statement is encountered within a loop, it immediately exits the loop, and the control flow continues with the statement immediately following the loop.

Example:

				
					#include <iostream>

int main() {
    for (int i = 1; i <= 5; ++i) {
        std::cout << i << " ";
        if (i == 3) {
            break; // Exit the loop when i equals 3
        }
    }
    std::cout << std::endl;
    std::cout << "Loop ended" << std::endl;
    return 0;
}

				
			
				
					// output //
1 2 3 
Loop ended

				
			

Explanation:

  • In this example, a for loop iterates from 1 to 5.
  • Inside the loop, each value of i is printed.
  • When i equals 3, the break statement is executed, causing the loop to terminate immediately.
  • As a result, only the values 1, 2, and 3 are printed, and the loop exits.
  • The statement "Loop ended" is printed after the loop because it is outside the loop’s scope.

Key Points:

  • The break statement is a powerful tool for controlling the flow of loops and is commonly used to exit loops prematurely when a specific condition is met.
  • It helps improve the efficiency of programs by avoiding unnecessary iterations.
  • The break statement can only exit the innermost loop in nested loop structures. If used in nested loops, it terminates the innermost loop only.
  • Be cautious when using the break statement, as excessive use or misuse can make code harder to understand and maintain.

continue; Statement

The continue statement in C++ is another control flow statement that is used within loops to skip the current iteration and move on to the next iteration of the loop. It allows you to bypass certain parts of the loop’s code block based on a specific condition, without terminating the loop entirely. Let’s delve deeper into the continue statement:

				
					continue;

				
			

Usage:

  • The continue statement can only be used inside loops, such as for, while, or do-while loops.
  • When the continue statement is encountered within a loop, it immediately jumps to the next iteration of the loop, skipping any remaining code in the loop’s body for the current iteration.
				
					#include <iostream>

int main() {
    for (int i = 1; i <= 5; ++i) {
        if (i == 3) {
            continue; // Skip iteration when i equals 3
        }
        std::cout << i << " ";
    }
    std::cout << std::endl;
    std::cout << "Loop ended" << std::endl;
    return 0;
}

				
			
				
					// output //
1 2 4 5 
Loop ended

				
			

Explanation:

  • In this example, a for loop iterates from 1 to 5.
  • Inside the loop, an if statement checks if i equals 3.
  • When i equals 3, the continue statement is executed, causing the loop to skip the remaining code for the current iteration and move to the next iteration.
  • As a result, the number 3 is skipped, and the loop continues normally with the values 1, 2, 4, and 5 being printed.
  • After the loop completes, the statement "Loop ended" is printed to indicate that the loop has finished executing.

Key Points:

  • The continue statement is useful for skipping certain iterations of a loop based on specific conditions, while still allowing the loop to continue executing.
  • It helps improve the efficiency of programs by avoiding unnecessary computations or operations for certain cases.
  • Like the break statement, the continue statement affects only the innermost loop in nested loop structures. If used in nested loops, it only skips the current iteration of the innermost loop.
  • It’s important to use the continue statement judiciously and ensure that it doesn’t lead to unintended logic errors or infinite loops.

Nested Loops and Break/Continue Statements

  • When used in nested loops, break and continue statements have a slightly different behavior compared to their use in single loops.
  • The break statement terminates only the innermost loop in which it is placed. It does not affect the outer loops.
  • The continue statement skips the current iteration of the innermost loop and moves to the next iteration of that loop.

Example of Break Statement in Nested Loops:

				
					#include <iostream>

int main() {
    for (int i = 1; i <= 3; ++i) {
        for (int j = 1; j <= 3; ++j) {
            if (i * j == 6) {
                std::cout << "Breaking the nested loop" << std::endl;
                break;
            }
            std::cout << i * j << " ";
        }
    }
    return 0;
}

				
			
				
					// output //
1 2 3 Breaking the nested loop
1 2 3 4 5 6 
1 2 3 4 5 6 

				
			

Explanation:

  • In this example, the break statement is placed inside the inner loop.
  • When the condition i * j == 6 is met, the break statement is executed, terminating the inner loop.
  • The outer loop continues its iterations as usual after the inner loop is terminated.

Example of continue Statement in Nested Loops:

				
					#include <iostream>

int main() {
    for (int i = 1; i <= 3; ++i) {
        for (int j = 1; j <= 3; ++j) {
            if (i == j) {
                std::cout << "Skipping iteration where i equals j" << std::endl;
                continue;
            }
            std::cout << i << "," << j << " ";
        }
    }
    return 0;
}

				
			
				
					// output //
1,2 1,3 Skipping iteration where i equals j
2,1 2,3 Skipping iteration where i equals j
3,1 3,2 Skipping iteration where i equals j

				
			

Explanation:

  • In this example, we have two nested for loops.
  • The outer loop iterates over the values of i from 1 to 3.
  • The inner loop iterates over the values of j from 1 to 3 for each iteration of the outer loop.
  • Inside the inner loop, there’s an if statement that checks if i equals j.
  • If i equals j, the continue statement is executed, skipping the remaining code inside the inner loop for that iteration.
  • As a result, when i equals j, the inner loop is skipped, and the next iteration of the outer loop begins.
  • The output shows the pairs of i and j values printed, except for the cases where i equals j, which are skipped due to the continue statement.

break and continue statements are powerful tools in C++ for controlling the flow of loops. They allow you to terminate loops prematurely or skip certain iterations based on specific conditions. By using these statements effectively, you can write more efficient and expressive code, improving the readability and maintainability of your programs. Happy coding! ❤️

Table of Contents

Contact here

Copyright © 2025 Diginode

Made with ❤️ in India