Break and continue statement in C++

In this tutorial, we will learn about ‘break and continue  in c++’.  Continue statement is use to skip the code block inside loop and still continue the loop. Whereas Break statement is used to jump out of the loop completely.

break and continue statement in c++


Break and continue statements in c++

To be able to understand this tutorial thoroughly, you must have knowledge on for loop, while and do while loop.

Break Statement


Syntax of break statement:

break;

Thats it, the syntax is just break.

In the loop, if above code break;  is encountered the loop terminates immediately.

Flowchart of break statement

Fig : flowchart of break statement

Where to use break statement

In while loop

while (test_condition)
{
  statement1;
  if (condition )
     break;
  statement2;
}

In do while loop

do
{
  statement1;
  if (condition)
     break;
  statement2;
}while (test_condition);

In for loop

for (int-exp; test-exp; update-exp)
{
  statement1;
  if (condition)
     break;
  statement2;
}

In all the above condition break is used inside if condition. 


The break statement terminates the loop immediately once it is encountered. Break statement is usually used inside if else  block.

Example of break statement

//program to count number until number is less than 10
# include <iostream>
using namespace std;

int main ()
{
 int num;
   cout << "enter the number: ";
   cin >> num;
 while (1)
 {
  
   if ( num > 10 ){
      break;
  }else{
       cout << num;
  }
   ++num;
 }
 return 0;
}

Output

enter the number: 3
3
4
5
6
7
8
9
10

Above example take a number input from user. It will print the number until the num is greater than 10. Once the num is greater than 10  the test expression num > 10  i.e 11>10 return true.

Hence the break command is executed. The loop then terminates. Break statement is also used with Switch case statement.

Continue Statement

The continue statement skips the code below continue  statement inside loop. But the loop is not terminated.


Syntax of continue Statement:

continue;

Flowchart of continue statement

fig : flowchart of continue statement

How to use continue statement

In while loop

while (test_condition)
{
  statement1;
  if (condition )
     continue;
  statement2;
}

In do while loop

do
{
  statement1;
  if (condition)
     continue;
  statement2;
}whi

In for loop

for (int-exp; test-exp; update-exp)
{
  statement1;
  if (condition)
     continue;
  statement2;
}

In above structures, if test condition return true then the continue statement will be executed which will interrupt the code block inside loop. However iteration of loop will be continued until loop expression return true. 

Example of Continue statement

//program to print sum of even number
# include <iostream>
using namespace std;

int main ()
{
 int a,sum = 0;
 for (a = 0; a < 10; a++)
 {
   
   if ( a % 2 != 0 ){
      continue;
      }
  sum = sum + a;
  }
 cout << "even number sum << " = "<< sum);
 return 0;
}

Output

even number sum = 25

In above example, if test expression a % 2 != 0  returns true i.e a is  the continue statement executes and the code below continue i.e                 

sum = sum + a;  is skipped and iteration is continued.