Break And Continue Statement In C++ Programming

 Hey everyone, welcome back to another Blog by AG in this session, we will learn about the break and continue statements in C++. Both are known as jump statements. These are called jump statements because they are used to skip jump or terminate the statements inside the loop, whether it is a while loop do while loop or a for Loop. So let's start with the break statement. First, in C++ break statements are used to terminate the execution of the loop. And by terminating the execution of loop, we means finishing or stopping the execution. If we want to block or terminate a loop at some point, all we need to do is to use the break statement with a condition on which we want the loop to stop, and it will terminate the loops flow or titrations. So break statements are mainly used for two cases. One is, it can be used inside the loop. And S is it is mostly used in switch statements. And this is the loops flow diagram in which first, there is a loop body. Then, if the condition is true, then there is a break statement which will terminate the loop. And if the condition is not true, then the loop will continue to titrate. As you can see in this example, the loop is right rating from 0 to 10 inside the loop.

There is a condition, and then the break statement, which will make sure that the loop will terminate when I becomes equal to 4 after the break statement, the control will jump out of the loop body, and that's how the break statement Works. Its output would be 0 1 2 3. It will not exceed four because of the break statement. Now coming to continue statement. The continue statement is a bit similar to the break statement, as it is also used for termination. But the difference is that it is used to terminate the current iteration and not the complete Loop. For example, if a loop is I'd rating from 0 to 6 and inside the loop, there is a continue statement on for then all the elements will be printed except for because continue statement skips that particular iteration and continues with the next one. And in break statement, the complete Loop flow terminates. So when the continue statement is used, then within the loop, the control skips or terminates that particular iteration and continues with the next iteration. As you can see in this example, first Loop body, then there is a condition. If that is true, then the continue statement will terminate and continues it with the next iteration. And if that is false, then control will go to the remaining part of the as always. Now, in this example, we are using continue statement here. The loop is I'd rating from 0 to 10 inside the loop. There is a condition, and then the a new statement which will make sure that if the value of I becomes for then terminate that I tration and moves to the next iteration, after that elements are getting printed with the help of see out function. Now, it's output would be 1 2 3 5 6 7 8 9 it will not display for because of continue statements, as we know that the continue statement skip that cooler titration. So when we use I equals to equals to 4 then it skips that particular iteration. If getting your learning started is half the battle. What if you could do that for free visit scale up by simply learn, click on the link in the description to know more. All right. So as we have covered the theoretical part. 

So now let's do some examples on break and continue statements in our code editor that is vs code. All right. So let's create a new file, and let's name it. Continue dot CPP alright, now let's save it. So now, first of all, we will include the header files iostream, then the namespace standard. So here we'll do an example in which will I treat a loop from 0 to 30, and we will display all the elements which are not divisible by 2 from 0 to 30. And here we will use a while loop. So let's start. So first of all, we will include the main function. And inside the main function, we will declare a variable I now, after that will use a wire while loop and will write I less than 30 inside it. This way. Loop will increment I plus plus because we want the loop to move forward. So that is why we are incrementing here. And now we will write the if statement and inside the if statement will write a condition which will exclude all those Elements which are divisible by 2. So we'll write I mod 2 20 equals to 0 then continue. Now here. What we are doing is we are skipping or excluding all these elements which are divisible by 2. Here we are displaying the elements and will write / T, read some horizontal space in between them, then here, right and L. And after that return, 0 alright. Now let's save it and check the brackets. First. 

Yes, the if condition inside this condition there is only continue statement. And this bracket is off while loop. And this is an extra bracket. Alright. Now, let's save it again. And let's try to run this. As you can see, 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 all of these are not divisible by 2, so we have skipped all those digits or numbers which are divisible by 2 with the help of this continue statement. Now let's do another example. Let's name it break dot C BB and let's save it. Now here we will do an example in which we will use both the continue and break statements. And in the example, we will display the cars which are taking part in the race. So first of all, let's include the header files. It's hot with hash include iostream. Now, using the namespace standardnow inside the main function. First of all, we will declare a car variable. Let's say the variable name is car number, all right, so here would be two types of races. One is drag race, and another one is lap race and the cars are denoted as car numbers. And there's a condition, if in the drag race, one player, even if one player is not present, then the race will be cancelled. And there is no such condition in lap Race. So now we'll start with drag race. So first of all, we will display drag race. Now, using the for Loop, we will I trait the card numbers from 0 to 10 as there are total 10 cars. So in car number 0 car number less than 10 card number plus, plus all right now, inside this for Loop we are using, IF condition, like, let's say, if car number 4 is not attending the race, then we'll right car car number 4 and inside the if condition will write break. If car number 4 is not attending the race, then there would be no reason as if one if only one car would back out, then there would be no race as per the condition. 

All right. So, as we can see, break will terminate the entire Loop. So it will display zero one, two, three. So it's the condition for the drag race. Now let's do for the lap race, maintain some space / n values. Now we will write lap race. No, inside this lap race will again use the for Loop as we have done above in car number from 0 to less than 10 card number plus plus before go to write n over here. Now, car number, plus, plus all right now, inside this for Loop will use if statement and write the same condition. That is, if car number is four car number is 4 then inside this will write. Continue, because we know in the lap race it is okay. If one or two players skips the race, the race won't stop, unlike the drag days. So simply will right, the variable name card number. And after that return, 0. All right, we're done now we can save it. And let's try to run this. Hope it works. All right, so there are some errors. So let's check Okay, so here we have used this extra bracket, because our for Loop bracket is here. No, let's save it again and run it. As you can see, drag race 0 1 2 3 so car number zero one two, three, and it is not exceeding four, because we have used this break statement, and in the lap race, as you can see, 0 1 2 3 5 6 7 8 9 as we have used, continue at car number 4 so car number 4 is getting skipped. So the rest of the cars will reach till the end. So in this example, we have used both continue and break statements. Now, in the next example, we will use a break statement since which case. So let's do that. Let's create a new file. And let's name it break 2 dot CPP. Alright. So here we will do an example of break statements in will use it in switch case statements. And the example is there are five contenders, and one of them won't. The lottery and the others came second, third and fourth. So we'll do that with the help of break statement. 

So let's start. First of all, we will include the header files, as include iostream, no, the namespace standard. Now, inside the main function, we will write inside Main Event Main and inside. This will declare a variable num equals 7 we are assigning 72 it. As of now, I'll tell you later. Now, using the switch statement inside the switch statement, we will write the variable name numb. Now, after that inside, this switch statement will make the cases, as we know, in Switched how the switch treatments work will make the cases like, let's say the first case is case for. So it means, if the value of num is for then, what shall we write inside this under this that will be displayed if the value of num is for all right. So we'll right loose, and after that and Dell, alright, then we will. Here we will write the break statement here. The break statement will stop the execution of the process. All right. Now, after that, similarly will make case. Another case is like, let's say, after this, we'll make another Case Case 6 now in the case 6 the person came forth. So there are prizes to others as well, but the first price is born by num seven as of now. Now another case would be case 7 now let's write 7 and right here you won't. No. After that another case would be, lets say one. Now, case eight. as we have selected seven num equal 7 so you won't. If, let's say we have selected four as we have assigned for now, save it again. So it will display you lose. And if we write 6 save it, then it will display came forth, as you can see. So this was another example of break statement in switch cases.