In This Article, You Will Learn about Java break and continue statement.
Java For Loop – Before moving ahead, let’s know a bit about Java for Loop.
Table of Contents
The break keyword
The break keyword is used to come out of the loop.
In other words, when the condition meets loop is stopped cause of the break keyword, and finally, the command comes out of the loop.
Example: Break the loop when i = 7.
public class Main {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
if (i == 7) {
break;
}
System.out.println(i);
}
}
}
Explanation: The variable i will start counting from 0 and keep incrementing by the number 1 in each iteration in the loop till i = 7. As i ==7, the condition will be true, therefore; the loop will break as per the condition.
Example: Break the loop when i = 5.
public class Main {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
if (i == 5) {
break;
}
System.out.println(i);
}
}
}
Explanation: The variable i will start counting from 0 and keep incrementing by the number 1 in each iteration in the loop till i = 5. As i ==5, the condition will be true, therefore; the loop will break as per the condition.
The continue keyword
The continue keyword keeps loop in interation and skip the value where condition is met and continues the next iteration in the loop.
Example: Skip the value i = 7 and continue the loop.
public class Main {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
if (i == 7) {
continue;
}
System.out.println(i);
}
}
}
Explanation: The variable i will start counting from 0 and keep incrementing by the number 1 in each iteration in the loop till i = 7. As i ==7, the condition will be true, therefore; the loop will skip this current value to print and continue the iteration as per the condition. In the final iteration, when i = 10, the condition will not be valid, and the loop will be stopped.
Example: Skip the value i = 5 and continue the loop.
public class Main {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
if (i == 5) {
continue;
}
System.out.println(i);
}
}
}
Explanation: The variable i will start counting from 0 and keep incrementing by the number 1 in each iteration in the loop till i = 5. As i ==5, the condition will be true, therefore; the loop will skip the current value to print and continue the iteration as per the condition. In the final iteration, when i = 10, the condition will not be valid, and the loop will be stopped.
Break and Continue in While Loop
In Java, we can use break and continue keyword in the while loop.
Example: Break the loop when i = 4.
public class Main {
public static void main(String[] args) {
int i = 0;
while (i < 10) {
System.out.println(i);
i++;
if (i == 4) {
break;
}
}
}
}
Explanation: The variable i will start counting from 0 and keep incrementing by the number 1 in each iteration in the loop till i = 4. As i ==4, the condition will be true, therefore; the loop will break as per the condition.
Example: Break the loop when i = 7.
public class Main {
public static void main(String[] args) {
int i = 0;
while (i < 10) {
System.out.println(i);
i++;
if (i == 7) {
break;
}
}
}
}
Explanation: The variable i will start counting from 0 and keep incrementing by the number 1 in each iteration in the loop till i = 7. As i ==7, the condition will be true, therefore; the loop will break as per the condition.
Example: Skip the value i = 6 and continue the loop.
public class Main {
public static void main(String[] args) {
int i = 0;
while (i < 10) {
if (i == 6) {
i++;
continue;
}
System.out.println(i);
i++;
}
}
}
Explanation: The variable i will start counting from 0 and keep incrementing by the number 1 in each iteration in the loop till i = 6. As i ==6, the condition will be true, therefore; the loop will skip the current value to print and continue the iteration as per the condition. In the final iteration, when i = 10, the condition will not be true, and the loop will be stopped.
Example: Skip the value i = 8 and continue the loop.
public class Main {
public static void main(String[] args) {
int i = 0;
while (i < 10) {
if (i == 8) {
i++;
continue;
}
System.out.println(i);
i++;
}
}
}
Explanation: The variable i will start counting from 0 and keep incrementing by the number 1 in each iteration in the loop till i = 8. As i ==8, the condition will be true, therefore; the loop will skip this current value to print and continue the iteration as per the condition. In the final iteration, when i = 10, the condition will not be valid, and the loop will be stopped.
If you find anything incorrect in the above-discussed topic and have further questions, please comment below.
Connect on: