There are two types of break statement,one is the unlabeled and other is labeled break. Following program is the example of unlabeled break. Just try to understand it.
Code:
package org.kodejava.example.lang;
public class BreakDemo {
public static void main(String[] args) {
int[] n1 = {8, 3, 9, 1, 9, 5, 4, 1, 9, 22};
int index1;
boolean found = false;
int search1 = 7;
for (index1 = 0; index1 < n1.length; index1++) {
if (n1[index1] == search) {
found = true;
break;
}
}
if (found) {
System.out.println(search + " found at index " + index1);
} else {
System.out.println(search + " was not found.");
}
start:
while (true) {
for (int x = 0; x < 10; x++) {
System.out.print("x = " + x + "; ");
if (x == 5) {
System.out.println("");
break start;
}
}
}
}
}
Bookmarks