Use of break&continue Keywords
break: Used in a switch...case statement or loop structure statement to end the current loop.
Example code:
1 Public classTestbreak {2 Public Static voidMain (string[] args) {3 for(inti = 1; I <= 5; i++){4 if(i% 4 = = 0){5 Break;//if the I-to 4 is satisfied zero, that is, I can be divisible by 4 when the break keyword is executed, jump out of the loop, the subsequent statements are not executed, in this loop i the maximum value is 5, so here only 4 can be divisible by 4 so the print statement will only print 1-3 of the value6 }7System.out.println ("i=" +i);8 }9 }Ten}
Use the break keyword in nested loops:
1 Public classTestbreak {2 Public Static voidMain (string[] args) {3 //two-layer loop4 for(inti = 1; I <= 5; i++){5 for(intj = 1; J <= 5; J + +){6 if(j% 4 = = 0){7 Break;//because it is a two-tier loop, and the break keyword is used in the inner loop, if the condition is met, it only jumps out of the inner loop and enters the outer loop to execute the statement again .8 }9System.out.print ("j=" +j+ "\ T");Ten //so the value of J is printed for the number of times the outer loop is specified, but the number after 4 is still not printed . One } A System.out.println (); - } - } the}
continue: Used in a loop structure statement to end the secondary loop.
Example code:
1 Public classTestcontinue {2 Public Static voidMain (string[] args) {3 //need to be separated from the break keyword, so the loop condition is changed to 10, so it's clearer to see the difference between continue4 for(inti = 1; I <= 10; i++){5 if(i% 4 = = 0){6 Continue;//if the I-to 4 is satisfied with zero, that is, I can be divisible by 4 when the Continue keyword is executed, ending this cycle, the subsequent statements of this loop are not executed, but the next loop statement will be executed if it does not satisfy the condition divisible by 4.7 }8System.out.print ("i=" +i+ "\ T");9 }Ten //after running, you will find that two numbers are not printed, but the subsequent failure to meet the criteria is printed out, and the break keyword is very different One } A}
Use the Continue keyword in nested loops:
1 Public classTestcontinue {2 Public Static voidMain (string[] args) {3 //two-layer loop4 for(inti = 1; I <= 5; i++){5 for(intj = 1; J <= 10; J + +){6 if(j% 4 = = 0){7 Continue;//because it is a two-tier loop, and the Continue keyword is used in the inner loop, if the condition is met, only the inner loop is ended and the next inner loop statement is executed .8 }9System.out.print ("j=" +j+ "\ T");Ten //so the value of J is printed for the number of times the outer loop is specified, but no number is printed that can be divisible by 4. One } A System.out.println (); - } - } the}
Add:
Both break and continue have a newly added feature that allows you to add a label to a layer when you want to use the break and continue keywords to end non-current layers in a multi-layered nested loop, which can be named by itself, such as the English label
You also need to label the FOR keyword in the loop layer you want to end with: The same label example--label:for (int i=0; loop condition; iteration) {}.
Java Foundation break and use of the Continue keyword