ITeye博客

使用一个break跳出多重循环

MoonMonster · 2015-11-20 09:16 · Java · 编程语言 · Markdown

大家都知道,java中的break是用来跳出循环的,例如

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

可以看到输出 0 1 2 3 4 5 很明显的这只是跳出一层循环,其实break的作用远不止于此。 例如,可以设置标签,让它在条件达到时,直接终止之后的循环,跳到标签出:

public class Test {

	public static void main(String[] args) {
		L:
		for(int i=0; i<10; i++){
			for(int j=0; j<10; j++){
				for(int k=0; k<10; k++){
					System.out.print(k + " ");
					if(k == 5){
						break L;
					}
				}
			}
		}
	}
}

会输出: 0 1 2 3 4 5 因为标签在最外层循环,所以整个三层循环都已经终止。 既然可以跳出最外层循环,那么也可以跳到任意一层循环中?试试, 为了使输出少一点,稍微改下循环次数:

public class Test {

	public static void main(String[] args) {
		for(int i=0; i<2; i++){
			L:
			for(int j=0; j<2; j++){
				for(int k=0; k<10; k++){
					System.out.print(k + " ");
					if(k == 5){
						break L;
					}
				}
			}
		}
	}
}

也可以看到预料的输出:0 1 2 3 4 5 0 1 2 3 4 5 那么不妨再多思考下,java的break能否像C语言的goto一样,跳到任意位置?同样用例子试下,第一次我们试着把标签放到整个循环之后去:

public class Test {

	public static void main(String[] args) {
		for(int i=0; i<2; i++){
			for(int j=0; j<2; j++){
				for(int k=0; k<10; k++){
					System.out.print(k + " ");
					if(k == 5){
						break L;
					}
				}
			}
		}
		
		L:
			System.out.println("跳到这儿了");
	}
}

但想法是好的,现实是残酷的,在编译时便已经报错: The label L is missing. 好吧,既然是找不到L标签的位置,那么我把L标签放到循环上方去好了:

public class Test {

	public static void main(String[] args) {
		L:
			System.out.println("跳到这儿了");
		System.out.println("接下来开始for循环:");
		for(int i=0; i<2; i++){
			for(int j=0; j<2; j++){
				for(int k=0; k<10; k++){
					System.out.print(k + " ");
					if(k == 5){
						break L;
					}
				}
			}
		}
	}
}

然后还是报了同样的错:The label L is missing. 此段程序跟第二段程序相比,只是中间多了段代码,如果把标签L与循环之间的输出语句去掉的话,可以看到就没有错了。 既然最外层循环与标签间不能有其他语句,那我在多层循环之间可否添加呢?

public class Test {

	public static void main(String[] args) {
		
		for(int i=0; i<2; i++){
			L:
				System.out.println("跳到这儿了");
			for(int j=0; j<2; j++){
				for(int k=0; k<10; k++){
					System.out.print(k + " ");
					if(k == 5){
						break L;
					}
				}
			}
		}
	}
}

同样的错:The label L is missing. 好吧,还是不行,那我再换种思路,既然标签是在for循环中使用的,那么我标签后再加上一个for循环可否?

public class Test {

	public static void main(String[] args) {
		
		for(int i=0; i<2; i++){
			L:
				for(int m=0; m<10; m++){
					
				}
			for(int j=0; j<2; j++){
				for(int k=0; k<10; k++){
					System.out.print(k + " ");
					if(k == 5){
						break L;
					}
				}
			}
		}
	}
}

同样的错:The label L is missing.

再多发散思维思考下,既然在for循环中可以这样使用,那么while循环也一样?

public class Test {

	public static void main(String[] args) {
		
		int i = 0;
		L:
		while(true){
			while(true){
				System.out.print(i+" ");
				i ++;
				if(i == 5){
					break L;
				}
			}
			
		}
	}
}

亲测可行,看到输出 0 1 2 3 4 。

所以可以得出以下结论: 1.break可以用来跳出一层循环。 2.如果加上标签,在break后添上标签名,可以用来跳出任意层循环。 3.标签与for语句之间不能有其他任何语句,即使是另一个for循环也不行。 4.java中的break不能像C语言中的goto一样使用。 5.for循环与while循环中的标签同样使用方法。

评论(2)

  1. #2 楼 · MoonMonster · 2015-12-11 19:56

    :? 再看,发现中间有段体现自己好无知。

  2. #1 楼 · BS_YG · 2015-11-29 16:48

    666,之前上网看socket的代码还奇怪loop是什么意思,这回懂了。 loop: while (true) { switch (c = in.read()) { case -1: case '\n': break loop; case '\r': int c2 = in.read(); if ((c2 != '\n') && (c2 != -1)) in.unread(c2); break loop; :D

评论功能维护中,当前仅展示历史评论。