在Java中,如何使用使用break
语句?
下面示例中,演示如何使用break
语句跳出循环(实现查找数值:1122
,找到后退出循环)
package com.yiibai;
public class UseOfBreakStatement {
public static void main(String[] args) {
int[] intary = { 199, 212, 252, 34, 5, 1122, 67, 5678, 8990 };
int no = 1122;
int i = 0;
boolean found = false;
for (; i < intary.length; i++) {
if (intary[i] == no) {
found = true;
break;
}
}
if (found) {
System.out.println("Found the no: " + no + " at index: " + i);
} else {
System.out.println(no + "not found in the array");
}
}
}
执行上面示例代码,得到以下结果 -
Found the no: 1122 at index: 5