Java中break的作用

1、最常见的作用,跳出Switch语句:

		int k;
		switch(k)
		{
			case 1:
			{
				System.out.println(1);
			}	break;
			case 2:
			{
				System.out.println(2);
				break;
			}
		}

2、跳出语句块:

		first:
		{
			second:
			{
				third:
				{
					for(int i=0;i<3;i++)
					{
						System.out.println(i);
						if(i==2)
						{
							break second;
						}
					}
				}
				System.out.println("Second");     //不执行
			}
			System.out.println("First");
		}

3、用out标记块,跳出:

<span style="white-space:pre">		</span>first:
		{
			out:
			{
				third:
				{
					for(int i=0;i<3;i++)
					{
						System.out.println(i);
						if(i==2)
						{
							break out;
						}
					}
				}
				System.out.println("out");
			}
			System.out.println("First");
		}


posted @ 2014-11-07 10:14  静以养身 俭以养德  阅读(1274)  评论(0编辑  收藏  举报