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.
- When the loop executes to the break statement, it exits the entire loop, and then executes the statement outside the loop.
-
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