Break Statement in a Loop – C++

What it does:
The break statement causes immediate exit from that statement. Program execuition continues with the next statement. Common usages are to exit early from a loop, skip the reminder of a switch statement.

#include <iostream>

using namespace std;


int main()
{
    int count;
    
    for (count =1; count<=20; count++)
    {
        if(count == 10)
        {
            break; // this will break out of the loop even though it was meant to run till 20
        }//if
        
        cout << count << endl;
    }//for
    
    cout << "The loop has ended at " << count << endl;
    
    return 0;
}//main

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s