distinguished between continue and break statement
Answers
Answer:
BREAK
- Break statement mainly used to terminate the enclosing loop such as while, do-while, for or switch statement wherever break is declared.
- Break statement resumes the control of the program to the end of loop and made executional flow outside that loop.
- As mentioned break is used for the termination of enclosing loop.
- Break statement can be used and compatible with 'switch', 'label'.
CONTINUE
- Continue statement mainly skip the rest of loop wherever continue is declared and execute the next iteration.
- Continue statement resumes the control of the program to the next iteration of that loop enclosing 'continue' and made executional flow inside the loop again.
- On other hand continue causes early execution of the next iteration of the enclosing loop.
- We can't use continue statement with 'switch','lablel' as it is not compatible with them.
Answer:
Both the statements are of a similar type, and they allow a user to alter/ make changes to the flow of a program. But they are still different. The primary difference between break and continue statement in C is that the break statement leads to an immediate exit of the innermost switch or enclosing loop. On the other hand, the continue statement begins the next iteration of the while, enclosing for, or do loop. In the cases of while and do loops, the continue statement immediately takes control of their test condition. Similarly, it takes control of the loop’s increment step in the case of a for a loop.
We can only apply the continue statement on the loops and not to any switch. When a continue is present inside a switch (further inside a loop), it causes the iteration of the next loop.
Practically, we can use a break in the switch if we want to exit after the execution of a particular case. The break also helps in the case of a loop whenever we wish to leave the loop after the occurrence of a certain condition. For example, if you prematurely reach the end of your data or reach an error condition. We use the continue statement only when we are willing to skip a statement (or more than one) in the body of a loop and transfer its overall control to its next iteration.