[Java] Two layers of for loop break out

1.break jumps out of the innermost for loop

For example

public class DoubleLoop {
	public static void main(String args[]){
		for(int i=0;i<5;i++){
			System.out.println("i="+i);
			for(int j=10;j<20;j++){
				System.out.println("j="+j);
				if(j==15)
					break;
			}
		}
	}
}

Running result: It can be seen that the outer loop is always in progress, and break only jumps out of the inner loop.



2.break wants to jump out of the two-layer loop

    Add a label before the outer loop, and specify the label when breaking, as follows:

public class DoubleLoop {
	public static void main(String args[]){
		outer:for(int i=0;i<5;i++){
				System.out.println("i="+i);
				for(int j=10;j<20;j++){
					System.out.println("j="+j);
					if(j==15)
						break outer;
				}
			}
	}
}

Running result: As you can see, it jumps out of the outer loop directly.


Guess you like

Origin blog.csdn.net/Crab0314/article/details/79312762