Break and Continue in C++

In this tutorial, we will learn about break and continue in C++.

Break and continue in C++

While writing some programs, there arise certain conditions where we would like to pass the control of the loop. It could be to either skip certain steps within the loop or exit from the loop entirely. In these cases, we use the continue and break statements. They are basically used to alter the flow of the program. The syntax for them is:

break;
continue;

Break statement

Break statement within a general loop jumps out of the smallest loop enclosing it. We also use break statement with a switch statement. It is to prevent from the execution of switch cases after the condition met and to exit from the switch condition entirely after the condition has been met.

#include<iostream>
using namespace std;
int main()
{
  for (int j = 1; j <= 4; j++)
  {
    cout << "j = " << j << endl;
    cout << "i loop " << endl;
    
                for (int i = 1; i <= 10; i++)
    {
      if (i == j + 4)
        break;  
      else
        cout << i << '\t';
    }
    cout << endl;
  }
  return 0;
}

Output :

j = 1
i loop
1  2  3  4
j = 2
i loop
1  2  3  4  5
j = 3
i loop
1  2  3  4  5  6
j = 4
i loop
1  2  3  4  5  6  7

Explanation :

In the above program, we use two loops. We use the break statement inside the second loop, and it doesn’t effect on the first loop. The first loop works for all its value starting from 1 to 4. For each value of j, the for loop for i works until the condition i = j + 4. Thus, when that condition is met and break statement executes, for loop for i is terminated.

Continue statement in C++

When a specified condition occurs, continue statement skips the statement after and loop continues with the next iteration. It is usually used in a conditional statement.

#include<iostream>
using namespace std;
int main()
{
  for (int j = 1; j <= 4; j++)
  {
    cout << "j = " << j << endl;
    cout << "i loop " << endl;
    for (int i = 1; i <= 10; i++)
    {
      if (i == j + 4)
        continue;
      else
        cout << i << '\t';
    }
    cout << endl;
  }
  return 0;
}

Output :

j = 1
i loop
1  2  3  4  6  7  8  9  10
j = 2
i loop
1  2  3  4  5  7  8  9  10
j = 3
i loop
1  2  3  4  5  6  8  9  10
j = 4
i loop
1  2  3  4  5  6  7  9  10

Explanation :

In the above program, we use two loops. We use the continue statement inside the second loop, and it doesn’t effect on the first loop. The first loop works for all its value starting from 1 to 4. For each value of j, the for loop for i works until the condition i = j + 4. When that condition is met and continue statement executes, the next iteration is started. It skips any output for that value of i. But, the loop continues to go for all the other values.

From the above two examples, we can also see that break statement terminates the smallest enclosing loop and the continue statement skips an iteration and jumps onto the next.

Hope this was helpful. Enjoy Coding!

Also learn :

Different Types of Constants in C++

Multithreading in C++

Leave a Reply

Your email address will not be published. Required fields are marked *