网站首页 > 文章精选 正文
Java编程语言中的break语句有以下两种用法 :
当在循环内遇到break语句时,循环立即终止,程序控制在循环体之后的下一个语句处重新开始。
它可以用于在switch语句中终止一个case(在下一章中介绍)。
语法
break的语法是循环内的单个语句 :
break;1复制代码类型:[java]
break语句流程图
示例1. break简单示例
public class Test {
public static void main(String args[]) {
int [] numbers = {10, 20, 30, 40, 50};
for(int x : numbers ) {
if( x == 30 ) {
break;
}
System.out.print( x );
System.out.print("\n");
}
}
}1234567891011121314复制代码类型:[java]
执行上面示例代码,得到以下结果 :
10
2012复制代码类型:[java]
示例2. break语句示例
在这个示例中,演示如何在java的for循环,while循环和do:while循环中使用break语句。
// package com.kaikeba.util;
public class JavaBreak {
public static void main(String[] args) {
String[] arr = { "Y", "I", "I", "B", "A", "I" };
// 在 for 循环中使用 break
for (int len = 0; len < arr.length; len++) {
if (arr[len].equals("I")) {
System.out.println("Array contains 'I' at index: " + len);
// 当找到字母时使用`break`语句中断循环
break;
}
}
// 在 while 循环中使用 break
int len = 0;
while (len < arr.length) {
if (arr[len].equals("B")) {
System.out.println("Array contains 'B' at index: " + len);
// 当找到字母时使用`break`语句中断循环
break;
}
len++;
}
len = 0;
// 在 do-while循环中使用 break
do {
if (arr[len].equals("A")) {
System.out.println("Array contains 'A' at index: " + len);
// 当找到字母时使用`break`语句中断循环
break;
}
len++;
} while (len < arr.length);
}
}12345678910111213141516171819202122232425262728293031323334353637383940复制代码类型:[java]
执行上面示例代码,得到以下结果:
Array contains 'I' at index: 1
Array contains 'B' at index: 3
Array contains 'A' at index: 4123复制代码类型:[java]
请注意,如果删除break语句,程序的输出将没有任何差异。 对于此示例中的小型迭代,没有的性能问题。 但是如果迭代器次数很大,那么它可以节省大量的处理时间。
示例3. Java break标签
break语句标签化用于终止外部循环,应该标记循环以使其起作用。这是一个演示java break标签语句用法的示例。
// package com.kaikeba.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;
// 查找第一个大于10的整数所在的索引值
searchint:
for (row = 0; row < arr.length; row++) {
for (col = 0; col < arr[row].length; col++) {
if (arr[row][col] > 10) {
found = true;
// 使用 break 标签来终止外部语句
break searchint;
}
}
}
if (found)
System.out.println("First int greater than 10 is found at index: [" + row + "," + col + "]");
}
}1234567891011121314151617181920212223242526复制代码类型:[java]
执行上面示例代码,得到以下结果:
First int greater than 10 is found at index: [3,0]
猜你喜欢
- 2025-06-30 个人C++笔记(2) 函数延伸(下)函数重载
- 2025-06-30 Qt开发经验小技巧181-185(qt开发程序)
- 2025-06-30 Linux编程Shell之入门—— for循环和for int循环详解
- 2025-06-30 Xi's Russia trip advances friendships, defends int'l equity: Chinese FM
- 2025-06-30 Int J Biol Macromol .|交联酶聚集体在分级共价有机骨架上的固定化:用于卤代醇不对称合成的高稳定酶纳米反应器
- 2025-06-30 C++ string/stack/list/deque/multiset 小结
- 2025-06-30 【 PLC知识分享】PLC中常说的位元件、字元件的区别
- 2025-06-30 Xi says China ready to work with Slovakia to address challenges, safeguard int'l justice
- 2025-06-30 模拟量万能公式,不管什么品牌的PLC都适用
- 2025-06-30 久坐引起的腰酸背痛,你以为站着就能解决?
- 06-30个人C++笔记(2) 函数延伸(下)函数重载
- 06-30Qt开发经验小技巧181-185(qt开发程序)
- 06-30Linux编程Shell之入门—— for循环和for int循环详解
- 06-30Xi's Russia trip advances friendships, defends int'l equity: Chinese FM
- 06-30Int J Biol Macromol .|交联酶聚集体在分级共价有机骨架上的固定化:用于卤代醇不对称合成的高稳定酶纳米反应器
- 06-30C++ string/stack/list/deque/multiset 小结
- 06-30【 PLC知识分享】PLC中常说的位元件、字元件的区别
- 06-30Xi says China ready to work with Slovakia to address challenges, safeguard int'l justice
- 最近发表
-
- 个人C++笔记(2) 函数延伸(下)函数重载
- Qt开发经验小技巧181-185(qt开发程序)
- Linux编程Shell之入门—— for循环和for int循环详解
- Xi's Russia trip advances friendships, defends int'l equity: Chinese FM
- Int J Biol Macromol .|交联酶聚集体在分级共价有机骨架上的固定化:用于卤代醇不对称合成的高稳定酶纳米反应器
- C++ string/stack/list/deque/multiset 小结
- 【 PLC知识分享】PLC中常说的位元件、字元件的区别
- Xi says China ready to work with Slovakia to address challenges, safeguard int'l justice
- 模拟量万能公式,不管什么品牌的PLC都适用
- 久坐引起的腰酸背痛,你以为站着就能解决?
- 标签列表
-
- newcoder (56)
- 字符串的长度是指 (45)
- drawcontours()参数说明 (60)
- unsignedshortint (59)
- postman并发请求 (47)
- python列表删除 (50)
- 左程云什么水平 (56)
- 计算机网络的拓扑结构是指() (45)
- 编程题 (64)
- postgresql默认端口 (66)
- 数据库的概念模型独立于 (48)
- 产生系统死锁的原因可能是由于 (51)
- 数据库中只存放视图的 (62)
- 在vi中退出不保存的命令是 (53)
- 哪个命令可以将普通用户转换成超级用户 (49)
- noscript标签的作用 (48)
- 联合利华网申 (49)
- swagger和postman (46)
- 结构化程序设计主要强调 (53)
- 172.1 (57)
- apipostwebsocket (47)
- 唯品会后台 (61)
- 简历助手 (56)
- offshow (61)
- mysql数据库面试题 (57)