Java Break & Continue
Java Tutorial » Java Break & Continue
Java Break & Continue are used to skip some statements inside the loop or terminate the loop immediately without checking the test expression.
Break & continue are called jump statements
Java Break: statement is used to terminate the loop immediately.
Example:
Output: 10
11
12
13
14
public class Main {
public static void main(String[] args) {
for (int x = 10; x < 22; x++) {
if (x == 15) {
break;
}
System.out.println(x);
}
}
}
public static void main(String[] args) {
for (int x = 10; x < 22; x++) {
if (x == 15) {
break;
}
System.out.println(x);
}
}
}
Output: 10
11
12
13
14
Java Continue, example
Continue statement is used to skip the current iteration of a loop.
We use continue statement inside any types of loops such as for, while, and do-while loop.
Example:
Output: 11
12
13
14
16
public class Main {
public static void main(String[] args) {
for (int x = 11; x < 17; x++) {
if (x == 15) {
continue;
}
System.out.println(x);
}
}
}
public static void main(String[] args) {
for (int x = 11; x < 17; x++) {
if (x == 15) {
continue;
}
System.out.println(x);
}
}
}
Output: 11
12
13
14
16
Break & Continue in While Loop
Example: Break Example
11
12
13
14
public class Main {
public static void main(String[] args) {
int x = 11;
while (x < 18) {
System.out.println(x);
x++;
if (x == 15) {
break;
}
}
}
}
Output: public static void main(String[] args) {
int x = 11;
while (x < 18) {
System.out.println(x);
x++;
if (x == 15) {
break;
}
}
}
}
11
12
13
14
Example: Continue Example
11
12
13
14
16
17
18
19
20
public class Main {
public static void main(String[] args) {
int x = 11;
while (x < 21) {
if (x == 15) {
x++;
continue;
}
System.out.println(x);
x++;
}
}
}
Output: public static void main(String[] args) {
int x = 11;
while (x < 21) {
if (x == 15) {
x++;
continue;
}
System.out.println(x);
x++;
}
}
}
11
12
13
14
16
17
18
19
20
java break: for loop, while, foreach, line, if, label, while loop, out of for loop, stream java break continue, return, label, if statement, for loo[, nesteed loop, example, condition
Java Break & Continue - java
Online Editor
This tool makes it easy to create, adjust, and experiment with custom colors for the web.
HTML Templates

Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.
CSS HTML Layout

Find here examples of creative and unique website layouts.
Free CSS HTML Menu

Find here examples of creative and unique website CSS HTML menu.