Break and continue are used to control the loop structure, mainly to stop the loop.
1.break
Sometimes we want to terminate the loop when a certain condition occurs, rather than wait for the loop condition to be false.
This is what we can do with break. Break is used to completely end a loop and jump out of the loop body to execute the statement following the loop.
2.continue
Continue and break are a bit similar, except that continue terminates the loop, and then executes the subsequent loop, and break terminates the loop completely.
It can be understood that continue is skipping the remaining statements in the secondary loop, performing the next loop.
Example:
[CPP]View PlainCopy
- #include <stdio.h>
- void Main ()
- {
- int sum,i;
- sum=0;
- For (i=1;i<=100;i++)
- {
- Sum=sum+i;
- if (i==2)
- {
- printf ("executive Continue");
- continue;
- }
- if (i==4)
- {
- printf ("Execution break");
- Break ;
- }
- printf ("sum=%d", sum);
- }
- printf ("loop over");
- }
The result of this operation is: Sum=1;
implementation of continue;
sum=6;
Execution break Loop End
Run end.
Original link: http://blog.csdn.net/abc5382334/article/details/17308375
The difference between break and continue