Difference between break and continue in C++
A loop control statement used to end a loop in C++ is called a break. The loop iterations end as soon as the break statement is met from inside the loop, and control is instantly transferred from the loop to the first statement after the loop.
Break statements are generally used when we are unsure of the precise number of loop iterations or when we wish to end the loop depending on a condition.
Here, we'll explore how to use three distinct loop types with break statements:
Now let's examine the break statement-based examples for each of the three loop types mentioned above.
Break with Simple loops
Think about a scenario where we need to find a certain element in an array. Use a loop to iterate through the array starting at the first index, and then compare the array members with the provided key to do this.
Element found at position: 3 …………………………………………… Process executed in 1.11 seconds Press any key to continue.
The aforementioned code executes without a hitch. However the aforementioned code is ineffective. Even when the element is located, the aforementioned code still completes all iterations. If the array contains 1000 elements and the key to be searched is present in the first place, the aforementioned technique will run 999 unnecessary iterations.
We may utilize the break statement in our software to prevent these pointless iterations. The control from the loop returns as soon as the condition is met after the break statement is reached. Thus, as shown below, we will combine the break statement with the if condition that compares the key with array elements:
Element found at position: 3 .................................................. Process executed in 1.11 seconds Press any key to continue.
Break with Nested Loops
In working with nested loops, break statements are also an option if the innermost loop contains the break statement. Just the innermost loop will release the control.
*** *** *** *** *** …………………….. Process executed in 1.11 seconds Press any key to continue.
We can see from the code above that the inner loop is set up to run for 10 iterations. Nevertheless, the inner loop ceases to run as soon as the value of j rises over 3, limiting the inner loop's iterations to just 3. The outer loop iteration, however, is unaffected. So, break only has an impact on the loop it is present in.
Break with Infinite Loops
For the purpose of ending the execution of an endless loop, the break statement can be used in conjunction with a condition.
Execution timed out .................................. Process executed in 1.11 seconds Press any key to continue.
The loop condition in the aforementioned program, which determines when to end the loop, is always true. This causes the loop to run indefinitely.
NOTE: Please refrain from running the aforementioned program in your compiler since it is in an indefinite loop and will likely require you to forcibly quit the compiler in order to end the program.
By use the break statement, as seen below, we can fix this:
1 2 3 4 5 6 7 8 9 10 ................................ Process executed in 1.11 seconds Press any key to continue.
The aforementioned code limits the loop iterations to ten. Apart from this, Switch case statements can also employ the break.
Continue statement in C
The C languages continue statement acts as a jump statement to move program control to the beginning of the loop. The while loop, for loop, or do while loop all have a continue statement that we may use to change the way the program normally runs. It cannot be used with a C switch case, in contrast to break.
When used, the C continue statement returns program control to the loop's start. As a result, the control shifts to the following iteration of the loop and skips the current one. The statements in the loop that follow the continue statement are not carried out.
Just placing the continue keyword anywhere we wish in the loop body constitutes the continue syntax.
Use of continue in C
Each type of loop in C can skip the current iteration by using the continue statement. It may be applied to the following kinds of loops in C:
Using continue in infinite loops is not useful as skipping the current iteration won't make a difference when the number of iterations is infinite.
Example of continue in C
Example 1: C A program that uses a single loop and the continue statement.
The for, while, and do-while loops all support the continue statement.
1 2 3 5 6 7 8 1 2 3 5 6 7 8 ......................... Process executed in 1. 11 seconds Press any key to continue.
Example 2: A program to utilize nested loops continues in C.
One loop at a time is all that the continue statement can handle. In order to skip the current iteration of the inner loop while utilizing nested loops, we may use the continue statement.
0 1 2 4 0 1 2 4 0 1 2 4 ............. Process executed in 1.11 seconds Press any key to continue.
When the continue statement is used in the program above, it skips the current inner loop iteration. As a consequence, the inner loop update expression manages the program. 3 is never seen in the output in this fashion.
The continue statement operates as follows:
Although while the statements used to expressly change the regular flow of a program break and continue are of the same kind, there are some differences between them.
An illustration of the distinction between a break and continue statement.
The loop with break produces output as: 1 2 The loop with continue produces output as: 1 2 4 5 …………………………………………………………………….. Process executed in 0.1 seconds Press any key to continue.
Explanation: In the program above, the first loop would display the value of I up to 3 and then stop since a break statement was used when I equaled 3. And when i is equal to 3, the second for loop will continue running without printing the result of i.