Signup/Sign In
LAST UPDATED: NOVEMBER 1, 2020

C++ Break Statement in Loop Program

Hello Everyone!

In this tutorial, we will learn how to demonstrate the concept of break statement in loops, in the C++ programming language.

Code:

#include <iostream>

using namespace std;

int main()
{
    cout << "\n\nWelcome to Studytonight :-)\n\n\n";
    cout << " =====  Program to understand the break keyword ===== \n\n";

    // Local variable declaration and initialization:
    int n = 10;

    // do loop execution
    do
    {
        cout << "Value of variable n is : " << n << endl;
        n = n + 1;
        if (n == 15)
        {
            // terminate the loop and execute the next statement following it.

            cout << "\n\nExecuting the break statement and terminating the loop.\n\n";
            break;
        }
    } while (n < 20);

    cout << "The value at which the loop got terminated is : " << n << "\n\n\n";

    return 0;
}

Output:

C++ break in loops

We hope that this post helped you develop a better understanding of the concept of break statement in loops, in C++. For any query, feel free to reach out to us via the comments section down below.

Keep Learning : )



About the author:
Nikita Pandey is a talented author and expert in programming languages such as C, C++, and Java. Her writing is informative, engaging, and offers practical insights and tips for programmers at all levels.