BREAK TO EXIT LOOPS


1.Using Break Statemet to Exit Loops

When you develop a loop it is possible to force program to exit this loop by using the break statement/command. When the break statement is encountered inside a loop, the loop is immediately terminated, and the program will continue executing with the first command after the loop.
Now I will illustrate the brake statement with the program which counts numbers from 0 to 1000000 but stops at the number 10000. First of all start a new Win32 Console Project and enter the following code.
// Break statement.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
            long double x;
            for (x = 0; x < 100000; x++) {
                        if(x == 10000) break;
                        cout << x << endl;
            }
            return 0;
}
If you entered this code than click on the green arrow (Start Debugging) and check the program for syntax errors. After you checked the program code press Ctrl+F5 to run the program. On your screen it should appear Win32 Console application like this.

This program will print the numbers from 0 to 9999 before ending. The program will not go to 100000 because the break statement will cause it to terminate early. The break statement is usually used in loops in which special condition can cause immediate termination.
Let’s take a closer look at the following fragment of the program code.
            for (x = 0; x < 100000; x++) {
                        if(x == 10000) break;
                        cout << x << endl;
            }
Before the for loop the variable x is define using the long double type of declaration. When program enters a for loop he will know that x equals zero and that x has to be smaller than 10000. Also the program will know that it can increase the value of variable x. This means that x can be a number from 0 to 99999. After the curly bracket { I have used the if statement and said that after the x value reaches 10000 the loop should terminate. This was done using the break statement. I should also mention that the cout and endl command is needed for printing all these numbers.
Now let’s take a look at another example. Start the new Win32 project and copy the following code.
// Break statement1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
            long double x, count;
            for (x = 0; x < 100000; x++) {
                        count = 3000;
                        for (;;) {
                                   cout << count << endl;
                                   count++;
                                   if(count == 6000) break;
                        }
                        cout << '\n';
            }
            return 0;
}
This program will print the numbers 3000 to 6000 on the screen 100000 times. Each time the break is encountered, control is passed back to the outer for loop.
Share on Google Plus