The main difference between break and continue in C++ is that the break is used to terminate the loop immediately and to pass the control to the next statement after the loop while, the continue is used to skip the current iteration of the loop.
C++ is a high level, general-purpose programming language. It is an advanced version of the C language. In other words, C++ is similar to C, but it supports Object Oriented Programming and has other additional features. Moreover, C++ is useful to write efficient programs and is used to develop operating systems, device drivers, embedded systems, image processing applications and for researching. In programming, sometimes it is necessary to repeat the same set of instructions again and again. Loops help to iterate a set of instructions a number of times. When executing a loop, sometimes it is necessary to skip statements inside the loop or to terminate the loop. Break and continue help in these situations.
Key Areas Covered
1. What is break in C++
– Definition, Functionality
2. What is continue in C++
– Definition, Functionality
3. What is the Difference Between break and continue in C++
– Comparison of Key Differences
Key Terms
C++, continue, break, OOP
What is break in C++
The break keyword helps to terminate a loop immediately. When there is a break statement while executing a loop, the control passes to the very next line after the loop. An example program is as follows.
According to the above program, for loop iterates from 1 to 5. When the ‘i’ value becomes 4, the test condition becomes true. Therefore, the break statement executes, and the loop terminates. As the loop terminates when ‘i’ is 4, the values after 3 will not print. It will only print 1, 2 and 3.
What is continue in C++
The continue keyword helps to skip the current iteration of the loop. Refer the below example program.
According to the above program, the loop iterates from 1 to 5. When the ‘i’ is 1, the remainder after dividing by 2 is 1. So, the condition becomes true. Therefore, the continue statement executes and the iteration skips to the next. But, when ‘i’ becomes 2. Reminder after dividing 2 by 2 is 0. Hence, the condition is false, and the continue does not execute. Therefore, the value 2 prints. In the next iteration, ‘i’ is 3. Dividing 3 by 2 gives the remainder 1. Thus, the condition is true. Therefore, continue executes and the iteration goes to next. Then, ‘i’ is 4 and this process occurs till ‘i’ is 5. If the remainder is 1, continue executes, and the iteration jumps to the next. Therefore, only even numbers are printed on to the screen.
Difference Between break and continue in C++
Definition
The break is a loop control structure that causes the loop to terminate and pass the program control to the next statement following the loop. The continue is a loop control structure that causes the loop to jump to the next iteration of the loop immediately. Hence, this explains the main difference between break and continue in C++.
Usage
Moreover, another difference between break and continue in C++ is that break helps to terminate the execution of the loop while continue helps to skip statements inside the loop.
Conclusion
In brief, break and continue are two keywords that help to control the repetition of a set of instructions in a program. The main difference between break and continue in C++ is that the break is used to terminate the loop immediately and to pass the control to the next statement after the loop while, the continue is used to skip the current iteration of the loop.
Reference:
1. “C Break Statement.” Www.tutorialspoint.com, Available here.
2. “C Break Statement.” Www.tutorialspoint.com, Available here.
Leave a Reply