In general, after the program enters the loop body and executes all the statements in the loop body before the next loop is judged, the break and continue statements can terminate the loop or ignore some loops.
Break : This statement causes the program to terminate the loop that contains it and to proceed to the next stage of the program (the statement following the entire loop), that is, instead of jumping to the next cycle cycle and exiting the loop. If the break statement is contained within a nested loop, it only jumps out of the innermost loop.
#include <stdio.h>int main () {int res=0;int i=0;int n=0;printf ("Test Break and continue\n"), for (i=0;i<6;i++) { printf ("start...\n"), if (i==3) { printf ("Break \ n"); break; printf ("Break after\n"); printf ("Continue \ n"); Continue; printf ("Continue after\n"); } printf ("Now i =%d\n", i);} printf ("Test over!!! \ n "); return 0;}
$ gcc-o BC bc.c$./bctest break and Continuestart...now i = 0start...now i = 1start...now i = 2start...break Test over!! !$
nested Loops (break):
#include <stdio.h>int main () {int res=0;int i=0;int n=0;printf ("Test Break and continue\n"), for (i=0;i<6;i++) { printf ("start...\n"); for (n=0;n<6;n++) {if (n==3) { printf ("Break \ n"); break; printf ("Continue \ n"); Continue; } printf ("Now n=%d\n", n);} if (i==3) { printf ("Break \ n"); break; printf ("Break after\n"); printf ("Continue \ n"); Continue; printf ("Continue after\n"); } printf ("Now i =%d\n", i);} printf ("Test over!!! \ n "); return 0;}
$ gcc-o BC bc.c$./bctest break and Continuestart...now n= 0now n= 1now n= 2break Now i = 0start...now n= 0now n= 1now n= 2break Now i = 1start...now n= 0now n= 1now n= 2break Now i = 2start...now n= 0now n= 1now n= 2break break test over!!! $
Continue: When this statement is in a looping statement, when the program runs to this statement, it does not continue the following statement in the loop body, but jumps to the next loop to perform the next loop. If the continue statement is contained within a nested loop statement, it affects only the innermost loop that contains it.
#include <stdio.h>int main () {int res=0;int i=0;int n=0;printf ("Test Break and continue\n"), for (i=0;i<6;i++) { printf ("start...\n"), if (i==3) { //printf ("Break \ n"); break; printf ("Break after\n"); printf ("Continue \ n"); Continue; printf ("Continue after\n"); } printf ("Now i =%d\n", i);} printf ("Test over!!! \ n "); return 0;}
$ gcc-o BC bc.c$./bctest break and Continuestart...now i = 0start...now i = 1start...now i = 2start...continue START...N ow i = 4start...now i = 5test over!!! $
Nested loops (Continue):
#include <stdio.h>int main () {int res=0;int i=0;int n=0;printf ("Test Break and continue\n "), for (i=0;i<6;i++) {printf (" start...\n "), for (n=0;n<6;n++) {if (n==3) {//printf (" bre AK \ n "); Break printf ("Continue \ n"); Continue }printf ("Now n=%d\n", n);} if (i==3) {//printf ("Break \ n"); Break printf ("Break after\n"); printf ("Continue \ n"); Continue printf ("Continue after\n"); }printf ("Now i =%d\n", i);} printf ("Test over!!! \ n "); return 0;}
$ gcc-o BC bc.c$./bctest break and Continuestart...now n= 0now n= 1now n= 2continue now n= 4now n= 5now i = 0start...now n= 0now n= 1now n= 2continue now n= 4now n= 5now i = 1start...now n= 0now n= 1now n= 2continue now n= 4now n= 5now i = 2s Tart...now n= 0now n= 1now n= 2continue now n= 4now n= 5continue start...now n= 0now n= 1now n= 2continue now n= 4now n= 5 Now i = 4start...now n= 0now n= 1now n= 2continue now n= 4now n= 5now i = 5test over!!! $