Dremendo Tag Line

break and continue Statement in C++

Loops in C++

In this lesson, we will understand what is brake and continue statement in C++ Programming along with some example.

What is break Statement in C++

The break statement in C++ is a loop control statement which is used to terminate the loop. When the break statement encountered from within a loop, the loop iterations stops there and control returns from the loop immediately to the first statement written outside the body of the loop.

The break statement is used in situations when we want to terminate the loop based on some condition.

Note: A break statement can be used in for loop, while loop and do while loop.

We will see here the usage of break statement with three different types of loops:

  • Simple for Loop
  • Nested for Loop
  • Infinite for Loop

Simple for Loop: Consider the situation where we want to exit from the loop when user enters a specific number.

Below is the example of using break with simple for loop:

#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    int i;
    for(i=2; i<=50; i=i+2)
    {
        if(i==10)
        {
            break;
        }
        cout<<i<<" ";
    }
    return 0;
}

Output

2 4 6 8

In the above example, we want to exit from the loop when the value of i is equal to 10. So, we have used a break statement inside the body of if statement which executes when the given condition is true, and the loop terminates.


Nested for Loop: We can also use break statement while working with nested loops. If the break statement is used in the innermost loop, the control will come out only from the innermost loop.

Below is the example of using break with nested for loop:

#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    int i,j;
    for(i=1; i<=5; i++)
    {
        for(j=1; j<=10; j++)
        {
            if(j>3)
            {
                break;
            }
            else
            {
                cout<<j<<" ";
            }
        }
        cout<<endl;
    }
    return 0;
}

Output

1 2 3
1 2 3
1 2 3
1 2 3
1 2 3

In the above example, we can clearly see that the inner loop is programmed to execute for 10 times. But as soon as the value of j becomes greater than 3 the inner loop stops executing which restricts the inner loop to run 3 times only. However, the iteration of the outer loop remains unaffected.

Therefore, break applies only to the loop within which it is present.


Infinite for Loop: The break statement can be included in an infinite loop with a condition to terminate the execution of the infinite loop.

Below is the example of using break with infinite for loop:

#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    int i=1;
    for(;;)
    {
        if(i>10)
        {
            break;
        }
        cout<<i<<" ";
        i++;       // it means i=i+1
    }
    return 0;
}

Output

1 2 3 4 5 6 7 8 9 10

In the above example, the for loop executes an infinite number of times. To terminate the loop, we have used a break statement which terminates the loop, when the value of i is greater than 10.

What is continue Statement in C++

The continue statement in C++ is also a loop control statement just like the break statement. The continue statement is opposite to that of break statement, instead of terminating the loop, it forces to execute the next iteration of the loop.

As the name suggests, the continue statement forces the loop to continue or execute the next iteration. When the continue statement executes in the loop, the code inside the loop following the continue statement will skip and the next iteration of the loop will begin.

Note: A continue statement can be used in for loop, while loop and do while loop.

Example

Consider the situation when you need to write a program which prints the number from 1 to 10 except the number 6. It is specified that you have to do this using loop and only one loop is allowed to use.

Here comes the usage of continue statement. What we can do here is we can run a loop from 1 to 10, and every time we have to compare the value of iterator with 6. If it equals to 6, we will use the continue statement to continue to next iteration without printing anything, otherwise, we will print the value.

Below is the implementation of above idea:

#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    int i;

    // we run a loop from 1 to 10
    for(i=1; i<=10; i++)
    {
        // If the value of i is equals to 6,
        // continue to next iteration
        // without printing anything
        if(i==6)
        {
            continue;
        }

        // otherwise print the value of i
        cout<<i<<" ";
    }
    return 0;
}

Output

1 2 3 4 5 7 8 9 10

In the above example, when the value of i becomes 6, then continue statement execute and the loop move to the next iteration without executing anything written after the continue statement.

video-poster