C++ Breaking Control Statements

There are three ways to break a control statement – break, continue and goto statements. In this article, you will learn about these statements in detail.

Advertisements

A control statement executes all statements within its own block, but sometimes it wants to stop and exit the block. In these cases, we introduce a breaking statement.

Three keywords are used to do the task. You may use anyone of them depending on the task.

  • break
  • continue
  • goto

break Statement

The break statement simply exit the block wherever placed. You can use break in switch-case, while loop or any suitable block of code.

For example,

switch(n)
{
    case 1:
        cout << "Orange" << endl;
    case 2:
        cout << "Apples" << endl;
        break;
}
        cout << "Mangoes" << endl;

If the case is 1, then the output will be Orange, Apples because there is no break statement in case 1, it does not terminate the block and move to the case 2.

Graphical Representation

break statement diagram
break statement diagram

Breaking Out of while Loop

The break statement can exit a while loop similar to a switch-case. As soon as the necessary condition is met to break the loop, the break statement comes out of a while loop.

int number;
sum = 0;
int i = 0;
while(i < 20)
{
          cout << "Enter a number:";
          cin >> number;
         if( number < 0)
        {
             cout << "Negative number entered! try again !" << endl;
             break;
       }
        sum = sum + number;
}
cout << "Sum =" << " " << sum << endl;

If the number is less than 0, the break statement exit the while loop immediately.

continue Statement

The does exactly opposite of break statement, it continues the loop without breaking it. When an error condition is met the continue does not exit the loop, goes to top of the loop again.

Advertisements

For example, consider the previous program with continue.

// Continue with while loop
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
    // Variable declaration
    
    int i,sum,number;
    
    //Initialization
    
    sum = 0;
    i = 0;
    
    //while loop begins
    
    while(i < 5)
    {
            cout << "Enter a Number:";
            cin >> number;
            
            //if condition for continue
            
            if(number < 0)
            {
                      cout << "Negative value! try again." << endl;
                      continue;
            }
            sum = sum + number;
            i++;
    }
    
    //Printing output
    
    cout << "Sum =" << " " << sum << endl;
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

Output:

Enter a Number:10
Enter a Number:-9
Negative value! try again.
Enter a Number:10
Enter a Number:10
Enter a Number:10
Enter a Number:10
Sum = 50

The output shows that the continue does not exit the loop, but skip the current iteration. It is sometimes useful in some C++ programs.

goto Statements

The goto statement is different from break statement, or continue statement. It jump from one location to another within the C++ program.

The general syntax for goto statement is here.

goto statement
goto statement

The type of goto statement shown above will create an infinite loop. That’s why goto is not popular method to break a loop.

Example Program:

begin:
while(x < 10)
{
      sum = sum + x;
      if(x == 0)
      {
          goto begin;
     }
}

In the above piece of code, the if condition will stop an infinite loop from happening. Hence, conditional statements can avoid such problems.



Advertisements

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.