The difference between the use of break and continue in PHP

Source: Internet
Author: User
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!!! $

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.