The break and continue statements are the jump statements that are used to skip some statements inside the loop or terminate the loop immediately without checking the test expression. These statements can be used inside any loops such as for, while, do-while loop.
When a break statement is encountered inside a loop, the loop is immediately terminated and the program control resumes at the next statement following the loop.
The Java break statement is used to break loop or switch statement. It breaks the current flow of the program at specified condition. In case of inner loop, it breaks only inner loop.
Syntax:
break;
Using break to exit a loop
// Java program to demonstrates the break statement
// break to exit a loop
class Simple{
public static void main(String args[]){
for(int i = 1; i <= 10; i++){
if(i == 3)
break;
System.out.println(i);
}
System.out.println("Exit from the loop");
}
}
Output:
1
2
Exit from the loop
// Java program to demonstrates the break statement
// break to exit a loop
class Simple{
public static void main(String args[]){
int i = 1;
while(i <= 10){
if(i == 5)
break;
System.out.println(i);
}
System.out.println("Exit from the while loop");
}
}
Output:
1
2
3
4
Exit from the loop
class Simple{
public static void main(String args[]){
int month = 8;
String monthString;
switch(month){
case 1: monthString = "January";
break;
case 2: monthString = "February";
break;
case 3: monthString = "March";
break;
case 4: monthString = "April";
break;
case 5: monthString = "May";
break;
case 6: monthString = "June";
break;
case 7: monthString = "July";
break;
case 8: monthString = "August";
break;
case 9: monthString = "September";
break;
case 10: monthString = "October";
break;
case 11: monthString = "November";
break;
case 12: monthString = "December";
break;
default: monthString = "Invalid month";
}
System.out.println(monthString);
}
}
Output:
August
In Java, the continue statement is used to skip the current iteration of the loop. It jumps to the next iteration of the loop immediately.
We can use continue statement with for loop, while loop and do-while loop as well.
// Java program to demonstrates the continue
// statement to continue a loop
class Simple{
public static void main(String args[]){
for(int i = 1; i <= 10; i++){
if(i == 3)
continue;
System.out.println(i);
}
System.out.println("Exit from the loop");
}
}
Output:
1
2
4
5
6
7
8
9
10
// Java program to demonstrates the continue
// statement to continue a loop
class Simple{
public static void main(String args[]){
int i = 1;
while(i <= 10){
if(i == 5)
continue;
System.out.println(i);
}
System.out.println("Exit from the while loop");
}
}
Output:
1
2
3
4
6
7
8
9
10
Break statement | Continue statement |
---|---|
The break statement is used to terminate the loop immediately. | The continue statement is used to skip the current iteration of the loop. |
Break stops the continuation of the loop. | continuation of loop. continue do not stop the continuation of the loop, it only stops the current iteration. |
The break can be used with switch, label. | Continue can not be executed with switch and labels. |
The break statement terminates the whole loop early. | The continue statement brings the next iteration early. |
It is used to unconditionally jump out of the loop | It is used to unconditionally jump to the next iteration of the loop, skipping the remaining statements of the current iteration. |