Java break语句,标签

Java break statement is used to terminate the loop in between it’s processing. We use break reserve keyword for breaking out of the loop in java program.

Java break语句用于终止其处理之间的循环。 我们使用break reserve关键字来打破java程序中的循环。

Java休息 (Java break)

There are two forms of break statement – unlabeled and labeled. Mostly break statement is used to terminate a loop based on some condition, for example break the processing if exit command is reached.

break语句有两种形式- 未标记标记 。 通常,break语句用于根据某些条件终止循环,例如,如果到达exit命令,则中断处理。

Unlabeled break statement is used to terminate the loop containing it and can be used with switch, for, while and do-while loops.

无标签的break语句用于终止包含它的循环,并且可以与switch,for,while和do-while循环一起使用。

闯入java示例 (break in java example)

Here is an example showing java break statement usage in for loop, while loop and do-while loop.

这是显示for循环,while循环和do-while循环中java break语句用法的示例。

package com.journaldev.util;

package com.journaldev.util;

public class JavaBreak {

	public static void main(String[] args) {
		String[] arr = { "A", "E", "I", "O", "U" };

		// find O in the array using for loop
		for (int len = 0; len < arr.length; len++) {
			if (arr[len].equals("O")) {
				System.out.println("Array contains 'O' at index: " + len);
				// break the loop as we found what we are looking for
				break;
			}
		}

		// use of break in while loop
		int len = 0;
		while (len < arr.length) {
			if (arr[len].equals("E")) {
				System.out.println("Array contains 'E' at index: " + len);
				// break the while loop as we found what we are looking for
				break;
			}
			len++;
		}

		len = 0;
		// use of break in do-while loop
		do {
			if (arr[len].equals("U")) {
				System.out.println("Array contains 'U' at index: " + len);
				// break the while loop as we found what we are looking for
				break;
			}
			len++;
		} while (len < arr.length);
	}

}

Note that if we remove break statement, there won’t be any difference in the output of the program. For small iterations like in this example, there is not much of a performance benefit. But if the iterator size is huge, then it can save a lot of processing time.

请注意,如果我们删除break语句,程序的输出将不会有任何差异。 对于此示例中的小迭代,性能没有太大好处。 但是,如果迭代器大小很大,则可以节省大量处理时间。

Java中断标签 (Java break label)

Labeled break statement is used to terminate the outer loop, the loop should be labeled for it to work.

标记为break的语句用于终止外部循环,应标记该循环才能使其正常工作。

Here is an example showing java break label statement usage.

这是显示java break标签语句用法的示例。

package com.journaldev.util;

public class JavaBreakLabel {

	public static void main(String[] args) {
		int[][] arr = { { 1, 2 }, { 3, 4 }, { 9, 10 }, { 11, 12 } };
		boolean found = false;
		int row = 0;
		int col = 0;
		// find index of first int greater than 10
		searchint:

		for (row = 0; row < arr.length; row++) {
			for (col = 0; col < arr[row].length; col++) {
				if (arr[row][col] > 10) {
					found = true;
					// using break label to terminate outer statements
					break searchint;
				}
			}
		}
		if (found)
			System.out.println("First int greater than 10 is found at index: [" + row + "," + col + "]");
	}

}

We can also use break statement to get out of switch-case statement, you can learn about all these in below video.

我们还可以使用break语句摆脱switch-case语句 ,您可以在下面的视频中了解所有这些信息。

演示地址

Reference: Oracle Documentation

参考: Oracle文档

翻译自: https://www.journaldev.com/980/java-break-statement-label

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值