Java Examples - break Keyword Usage
The Java break statement can directly and forcibly exit the current loop, ignoring any other statements in the loop body and the loop condition test.
The following example uses the break keyword to break out of the current loop:
Main.java File
public class Main {
public static void main(String[] args) {
int[] intary = { 99,12,22,34,45,67,5678,8990 };
int no = 5678;
int i = 0;
boolean found = false;
for ( ; i < intary.length; i++) {
if (intary[i] == no) {
found = true;
break;
}
}
if (found) {
System.out.println(no + "The index position of the element is:" + i);
}
else {
System.out.println(no + "The element is not in the array");
}
}
}
The above code produces the following output:
5678 元素的索引位置在: 6Other Extensions
Java Examples