jump-statement;
break;
public class breakExample {
public static void main(String[] args) {
//using for loop
for (int i = 1; i <= 10; i++) {
if (i == 5) {
//breaking the loop
break;
}
System.out.println(i);
}
}
}
1
2
3
4
public class breakExample2 {
public static void main(String[] args) {
//outer loop
for (int i = 1; i <= 3; i++) {
//inner loop
for (int j = 1; j <= 3; j++) {
if ((i == 2) && (j == 2)) {
//using break statement inside the inner loop
break;
}
System.out.println(i + " " + j);
}
}
}
}
1 1
1 2
1 3
2 1
3 1
3 2
3 3
public class breakExample3 {
public static void main(String[] args) {
aa:for (int i = 1; i <= 3; i++) {
bb:for (int j = 1; j <= 3; j++) {
if ((i == 2) && (j == 2)) {
//using break statement with label
break aa;
}
System.out.println(i + " " + j);
}
}
}
}
1 1
1 2
1 3
2 1
public class breakwhileExample {
public static void main(String[] args) {
//while loop
int i = 1;
while (i <= 10) {
if (i == 5) {
//using break statement
i++;
break; //it will break the loop
}
System.out.println(i);
i++;
}
}
}
1
2
3
4
public class breakdowhileExample {
public static void main(String[] args) {
//declaring variable
int i = 1;
//do-while loop
do {
if (i == 5) {
//using break statement
i++;
break; //it will break the loop
}
System.out.println(i);
i++;
} while (i <= 10);
}
}
1
2
3
4