Course Content
C++ Introduction
0/1
C++ Variables & Constants
0/1
C++Scope of Variable
0/1
C++ Keywords & Identifiers
0/1
C++ Data Types
0/1
C++ Basic I/O
0/1
C++ Type Conversion
0/1
C++ Operators
0/1
C++ Comments
0/1
C++ If-else
0/1
C++ Ternary Operator
0/1
C++ for Loop
0/1
C++ Ranged for Loop
0/1
C++ while/do-while Loop
0/1
C++ break Statement
0/1
C++ Continue Statement
0/1
C++ switch Statement
0/1
C++ goto Statement
0/1
C++ Functions
0/1
C++ User-defined Functions
0/1
C++ Default Arguments
0/1
C++ Storage Class
0/1
C++ Recursion
0/1
C++ Return by Reference
0/1
C++ Arrays
0/1
C++ Multi-dimentional Arrays
0/1
C++ Arrays & Function
0/1
C++ String
0/1
C++ Structure
0/1
C++ Structure & Functions
0/1
C++ Pointers to Structure
0/1
C++ Pointers
0/1
C++ Void Pointers
0/1
C++ Pointers & Arrays
0/1
C++ Pointers & Functions
0/1
C++ Dynamic Memory Allocation
0/1
C++ OOPs Concepts
0/1
C++ Objects and Class
0/1
C++ Constructors
0/1
C++ Destructors
0/1
C++ Constructor Overloading
0/1
C++ Objects & Function
0/1
C++ Enumeration
0/1
C++ Inheritance
0/1
C++ Inheritance Access Control
0/1
C++ Inheritance Types
0/1
C++ Polymorphism
0/1
C++ Function Overloading
0/1
C++ Function Overriding
0/1
C++ Operator Overloading
0/1
C++ Friend Function
0/1
C++ Virtual Function
0/1
C++ Abstract Class & Pure Virtual Function
0/1
C++ Encapsulation
0/1
C++ Abstraction
0/1
C++ Templates
0/1
C++ Exception Handling
0/1
C++ Multithreading
0/1
C++ Standard Library
0/1
C++ Programming Tutorials
About Lesson

C++ break Statement

In this tutorial, we will learn about the break statement and its working in loops with the help of examples.


Break Statement

  • The break is a loop control statement which is used to terminate the loop.
  • It is used to stop the current execution and proceed with the next one.
  • Basically break statements are used in the situations when we are not sure about the actual number of iterations for the loop or we want to terminate the loop based on some condition.
  • When the break statement is called by a compiler, it immediately stops the execution of the loop and transfers the control outside the loop and executes the other statements.

In C++, the break statement terminates the loop when it is encountered.

The syntax of the break statement is:

break;

To understand break statement, you should have the knowledge of the following C++ programming topics:

  • C++ if, if-else and Nested if-else Statement
  • C++ for Loop
  • C++ while/do-while Loop

Working of C++ break Statement

Working of C++ break Statement
Working of break statement in C++

Example 1: break statement with for loop

// program to use break statement inside for loop

#include <iostream>
using namespace std;

int main() {
    for (int i = 1; i <= 8; i++) {
        // break condition     
        if (i == 5) {
            break;
        }
        cout << i << endl;
    }
    return 0;
}

Output

1
2
3
4

This means, when i is equal to 5, the break statement terminates the loop. Hence, the output doesn’t include values greater than or equal to 5.

Note: The break statement is usually used with decision-making statements.


Example 2: C++ break statement with while loop

// program to find the sum of positive numbers
// if the user enters a negative numbers, break ends the loop

#include <iostream>
using namespace std;

int main() {
    int num;
    int sum = 0;

    while (true) {
        // take input from the user
        cout << "Enter a number: ";
        cin >> num;

        // break condition
        if (num < 0) {
            break;
        }
        // add all the positive integers
        sum += num;
    }
    // display the sum
    cout << "The sum of positive numbers: " << sum << endl;
                                
    return 0;
}

Output

Enter a number: 3
Enter a number: 4
Enter a number: 5
Enter a number: -4
The sum of positive numbers: 12

In the above program, the user enters a number. The while loop is used to print the total sum of numbers entered by the user. Here, notice the code,

if(num < 0) {
    break;
}

This means, when the user enters a negative number, the break statement terminates the loop and codes outside the loop are executed.

The while loop continues until the user enters a negative number.


Example 3: break statement with Nested loop

When break is used with nested loops, break statement terminates the inner loop. For example,

// using break statement inside nested for loop

#include <iostream>
using namespace std;

int main() {
    int num;
    int sum = 0;

    // nested for loops

    // first loop
    for (int i = 1; i <= 3; i++) {
        // second loop
        for (int j = 1; j <= 2; j++) {
            if (i == 2) {
                break;
            }
            cout << "i = " << i << ", j = " << j << endl;
        }
    }
                                
    return 0;
}

Output

i = 1, j = 1
i = 1, j = 2
i = 3, j = 1
i = 3, j = 2

In the above program, the break statement is executed when i == 2. It terminates the inner loop, and the control flow of the program moves to the outer loop.

Hence, the value of i = 2 is never displayed in the output.


Example 4: break statement with do-while loop

// C++ Program to print break statement inside do-while loop

#include <iostream>
using namespace std;
                                  
int main() {
    // Initialization expression
    int i = 5; 
                                  
    // do...while loop execution
    do {
        cout << "Value of i: " << i << endl ;
        i = i + 1;
        if (i > 10){
            // terminate the loop
            break;
        }
    }while (i <= 12);
                                
    return 0;
}
Value of i: 5
Value of i: 6
Value of i: 7
Value of i: 8
Value of i: 9
Value of i: 10

In the above program, the break statement is executed when i > 10. It terminates the inner loop, and the control flow of the program moves to the outer loop.

Hence, the value of i > 2 is never displayed in the output.


The break statement is also used with the switch statement. To learn more, visit C++ switch statement.

Exercise Files
No Attachment Found
No Attachment Found
error: Content is protected !!