How to use break and continue in Java and its differences

Source: Internet
Author: User

Break

Break can be used for loops and switch...case ... Statement.

Used in Switch...case:

After executing the content that satisfies the case condition, end the switch without executing the following statement.

eg

 PublicStaticvoidBreakSwitch1 () {intn = 1; Switch(n) { Case1: System.out.println ("This is one.");  Break;  Case2: System.out.println ("This is a.");  Break; default: System.out.println ("Others."); }      }  

Results:

This is one.

EG2:

 PublicStaticvoidBreakSwitch2 () {intn = 1; Switch(n) { Case1: System.out.println ("This is one."); //Break ;  Case2: System.out.println ("This is a.");  Break; default: System.out.println ("Others."); }  }  

Results:

This is one.

This is a.

If you do not use the break statement, all operations will output after the first statement that satisfies the condition until a break statement is encountered;

I. Roles and differencesThe effect of break is to jump out of the current loop block (for, while, do, or block (switch). The function in the loop block is to jump out of the loop body that is currently circulating. The function in the block is the comparison between the interrupt and the next case condition. The Continue is used to end the execution of subsequent statements in the loop body and to jump back to the beginning of the loop block to perform the next loop instead of immediately looping the body.
    1. When the loop executes to the break statement, it exits the entire loop, and then executes the statement outside the loop.
    2. When the Loop statement executes to continue, the next cycle is restarted when the secondary loop ends. If this is the last cycle, then this is the same continue as the break effect.

How to use break and continue in Java and its differences

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.