Continue Statement In C++ and Break Statement

When we talk about Break and Continue Statement In C++ Programming. First of all, we talk About loops. The Loops chapter >> Loops and its usage.

So, You have already seen the break statement in the earlier chapter of this tutorial. its work is to jump out in the Switch statement.

The break statement can use to jump out from a loop. So Example is below, Jumps out of the loops when x is equal to 6.

#include <iostream>
using namespace std;

int main()
{
     for(int x=0; x<10; x++)
     {
         if(x==6)
         {
             break;
         }
         cout << x << endl;
     }
    return 0;
}

This code Paste in your compilers. So, compile and Run this for your practice the answer is 1 to 5 values.

Continue Statement In C++

So, the continue statement breaks the one iteration in a loop. if a specific condition occurs then the loop will continue with the next iteration in the loops. So Example is below:

#include <iostream>
using namespace std;

int main()
{
     for(int x=0; x<10; x++)
     {
         if(x==6)
         {
             continue;
         }
         cout << x << endl;
     }
    return 0;
}

Then, see the output in the next block below:

0
1
2
3
4
5
7
8
9

Infexo Service Like on Facebook is a WordPress Website Developer Team.

Break and Continue loop in While loop at the same loop

So, you can use the break and continue in one while loop. All the explanations and example is below explained.

#include <iostream>
using namespace std;

int main()
{
    int i = 0;
	while (i < 10) 
	{
  		if (i == 4) 
		{
    		i++;
			continue;
  		}
  		if (i == 6) 
		{
    		break;
  		}
  		cout << i << "\n";
  		i++;
	}
    return 0;
}

The Output is:

0
1
2
3
5

In this code of c++, I told you that, you can use both break and continue in the While loop. According to this method.

By Usama Arshad

It's me Usama Arshad working as a Lecturer C++. I am a student of BSIT.

Leave a Reply

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