summary of use of break and Continue keywords in Java
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.
second, other usesBreak and continue can be used with statement labels. This is very simple, the following to a comprehensive example, to see the understanding:
/**
* Created by IntelliJ idea.
* User:leizhimin
* date:2007-11-29
* TIME:15:47:20
*/
PublicclassTest {
PublicStaticvoidMain (String args[]) {
Test test =NewTest ();
TEST.TESTBREAK1 ();
Test.testcontinue1 ();
TEST.TESTBREAK2 ();
Test.testcontinue2 ();
}
/**
* Test Continue
* Continue used to end this cycle
*/
PublicvoidTestContinue1 () {
System.out.println ("--------Test continue-------");
for(inti = 1; I <= 5; i++) {
if(i = = 3)Continue;
System.out.println ("i="+ i);
}
}
/**
* Break to end the entire loop body
*/
Public voidTestBreak1 () {
System.out.println ("--------test break1-------");
for(inti = 1; I <= 5; i++) {
if(i = = 3) Break;
System.out.println ("i="+ i);
}
}
/**
* Test a tagged break statement
* tags can only be written in the loop body before, by the way to learn about the definition and use of statement tags in Java
*/
PublicvoidTestBreak2 () {
System.out.println ("--------test break2-------");
inti = 1;
intK = 4;
Lable1:
for(; I <= 5; i++, k--) {
if(k = = 0) BreakLable1;
System.out.println ("i="+ i +" ; k= "+ k);
}
}
PublicvoidTestContinue2 () {
System.out.println ("--------test continue2-------");
Lable1:
for(inti = 1; I < 10; i++) {
Lable2:
System.out.println ("i="+ i);
for(intj = 0; J < 10; J + +) {
if(j = = 9)ContinueLable1;
}
}
}
}Operation 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
Process finished with exit code 0 turn from: http://lavasoft.blog.51cto.com/62575/52685
Summary of use of break and Continue keywords in Java