1.break
Break is used to completely end a loop and jump out of the loop body. Regardless of the loop, once a break is encountered in the loop body, the system will completely end the loop
In Java is the label definition, the tag is a colon immediately following the English (:) identifier
CODE schematic:
Packagecom.oliver.test; Public classTestbreak { Public Static voidMain (string[] args) {//outer Loop, outside as identifierOutside: for(inti=0;i<=5;i++){ //Memory Loops for(intj=0;j<=5;j++) {System.out.println (The value of "I" is "+i+" and the value of J is "+j); //if the value of J is 4, then the outer loop is ended if(j==2){ Breakoutside; } } } }}
Printing results:
The value of I is 0,j with a value of 0
The value of I is 0,j with a value of 1
The value of I is 0,j with a value of 2
2.continue
Continue function and break a bit similar, the difference is that continue just ignore the remainder of the loop, and then start the next loop, does not terminate the loop, and break is to completely terminate the loop itself.
CODE schematic:
Packagecom.oliver.test; Public classTestcontinue { Public Static voidMain (string[] args) {//outer Loop, outside as identifierOutside: for(inti=0;i<=5;i++){ //Memory Loops for(intj=0;j<=5;j++) {System.out.println (The value of "I" is "+i+" and the value of J is "+j); //if the value of J is 2, then the outer loop is ended if(j==2){ Continueoutside; } } } }}
Printing results:
The value of I is 0,j with a value of 0
The value of I is 0,j with a value of 1
The value of I is 0,j with a value of 2
The value of I is 1,j with a value of 0
The value of I is 1,j with a value of 1
The value of I is 1,j with a value of 2
The value of I is 2,j with a value of 0
The value of I is 2,j with a value of 1
The value of I is 2,j with a value of 2
The value of I is 3,j with a value of 0
The value of I is 3,j with a value of 1
The value of I is 3,j with a value of 2
The value of I is 4,j with a value of 0
The value of I is 4,j with a value of 1
The value of I is 4,j with a value of 2
The value of I is 5,j with a value of 0
The value of I is 5,j with a value of 1
The value of I is 5,j with a value of 2
3.return
The return keyword is used to end a method and is not intended to end the loop.
CODE schematic:
Package com.oliver.test; Public class Testreturn { publicstaticvoid main (string[] args) { // a simple for loop for (int i=0;i<=3;i++) { System.out.println ( the value of "I" is: "+i ); if (i==2) { return; }}}
Printing results:
The value of I is: 0
The value of I is: 1
The value of I is: 2
"Java" Java_16 control loop structure break, Continue, Return