代码

public class Demo {
	public static void main(String[] args) {
//单层forxun循环(不使用标记)
		for (int x = 0; x < 10; x++) {
			System.out.print(x + " ");
			if (x == 5)
				break;
		}
		System.out.println();
		System.out.println("-------------1------------");
//单层forxun循环(使用标记)
		tag: for (int x = 0; x < 10; x++) {
			System.out.print(x + " ");
			if (x == 5)
				break tag;
		}
		System.out.println();
		System.out.println("------------2-------------");
//双层forxun循环(不使用标记)
		for (int x = 0; x < 10; x++) {
			for (int y = 0; y < 10; y++) {
				System.out.print(x + ":" + y + " ");
				if (x == 5)
					break;
			}
			System.out.println();
		}
		System.out.println();
		System.out.println("-----------3--------------");
//双层forxun循环(标记在外循环)
		outtag: for (int x = 0; x < 10; x++) {
			for (int y = 0; y < 10; y++) {
				System.out.print(x + ":" + y + " ");
				if (x == 5)
					break outtag;
			}
			System.out.println();
		}
		System.out.println();
		System.out.println("------------4-------------");
//双层forxun循环(标记在内循环)
		for (int x = 0; x < 10; x++) {
			intag: for (int y = 0; y < 10; y++) {
				System.out.print(x + ":" + y + " ");
				if (x == 5)
					break intag;
			}
			System.out.println();
		}

	}

}

 

结果:

break配合标记使用

 

说明:break tag 会中断循环到标记处,但不重新进入迭代。

之前一直没注意这个标记,记录下来当作笔记

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-06-23
  • 2021-06-15
  • 2021-12-15
  • 2022-12-23
  • 2022-12-23
  • 2022-01-08
猜你喜欢
  • 2021-04-21
  • 2021-12-04
  • 2022-12-23
  • 2022-12-23
  • 2021-06-08
  • 2022-12-23
  • 2021-09-05
相关资源
相似解决方案