Summary of the use of break and Continue keywords in Java _java

Source: Internet
Author: User

I. Functions and differences

The function of a break is to jump out of the current loop block (for, while, do), or a program block (switch). The function in the loop block is to jump out of the loop body that is currently circulating. The function in the program block is the comparison between the interrupt and the next case condition.

Continue is used to end the execution of subsequent statements in the loop body and to skip back to the beginning of the loop block to perform the next loop, rather than looping the body immediately.

Second, other uses

Break and continue can be used with the statement label.

This is very simple, here is a comprehensive example to see how:

/** * Created by the IntelliJ idea.  * User:leizhimin * date:2007-11-29 * TIME:15:47:20 * * public class Test {public static void main (String args[]) 
    {Test test = new test (); 

    TEST.TESTBREAK1 (); 

    Test.testcontinue1 (); 
    TEST.TESTBREAK2 (); 
  Test.testcontinue2 (); /** * Test Continue * Continue to end this cycle/public void testContinue1 () {System.out.println ("------- 
    -Test continue-------"); 
      for (int i = 1; I <= 5; i++) {if (i = = 3) continue; 
    System.out.println ("i=" + i); }/** * Break is used to end the entire loop body/public void TestBreak1 () {System.out.println ("--------Test break1-------" 
    ); 
      for (int i = 1; I <= 5; i++) {if (i = = 3) break; 
    System.out.println ("i=" + i); }/** * Test tagged break statement * Tags can only be written in the loop body before, by the way, learn Java statement label definition and use/public void TestBreak2 () {Syste 
    M.out.println ("--------test break2-------"); 
    int i = 1; 
    int k = 4; Lable1:for (; I <= 5; i++, k--) 
      {if (k = = 0) break lable1; System.out.println ("i=" + i +); 
    k= "+ K"); 
    } public void TestContinue2 () {System.out.println ("--------test continue2-------"); 
      lable1:for (int i = 1; i < i++) {lable2:System.out.println ("i=" + i); 
      for (int j = 0; J < + j) {if (j = = 9) continue lable1; } 
    } 
  } 
}

Run Result:

--------Test break1------- 
i=1 
i=2 
--------test continue------- 
i=1 
i=2 i=4 i=5 
---- ----Test BREAK2-------i=1; k=4 i=2; k=3 i=3; k=2 i=4; k=1 
--------test continue2------- 
i= 1 
i=2 
i=3 
i=4 
i=5 
i=6 
i=7 i=8 i=9 

The above is a small series for everyone to bring the break in Java and the use of Continue keyword summary of all the content, I hope that we support cloud Habitat Community ~

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.