The difference between break and continue and the use of label labels

Source: Internet
Author: User

Break means to jump directly out of the current loop, and break can only be applied to switch--case statements and loops

Continue means jumping out of the loop and continuing with the next loop

The label tag can choose to break, or continue for which loop statement

For example, to output all numbers from 1 to 10, loop out four rows.

If the i%4==0 break;

If I%4==0 is continue;

1 classtestbreakcontinue2 {3      Public Static voidMain (string[] args)4     {5         /*6 for (int i=1;i<5;i++) {7 for (int j=1;j<=10;j++) {8 if (j%4==0) {9 break;//End Current inner for loop ①Ten //continue;//End the secondary inner for Loop and continue with the next loop ② One                 } A System.out.print (j); -             }     - System.out.println (); the         } -          -         */ -Label for(inti=1;i<5;i++) {//Define a tag label +              for(intj=1;j<=10;j++){ -                 if(j%4==0){ +                     //Break label;//ends the current outer for loop ③ A                     ContinueLabel//end the second outer for loop, and proceed to the next loop ④ at                 } - System.out.print (j); -             } - System.out.println ();  -         } -          in     } -}

Running Result: ④

The difference between break and continue and the use of label labels

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.