JAVA TUTORIAL

Java Intro Java Features Java vs C++ Java Simple Program Java Applet vs Application JVM,JDK or JRE Java Keywords Java Variables Java Literals Java Separators Java Datatypes Java Operators Java Statements Java Array Java String

CONTROL STATEMENTS

Java If-else Java While Loop Java Do-While Loop Java For Loop Java For-each Loop Java Switch Java Break/Continue

JAVA METHODS

Java Method Java Overloading Java Overriding

JAVA CLASSES

Java OOPs Java Classes/Objects Java Inheritance Java Polymorphism Java Encapsulation Java Abstraction Java Modifiers Java Constructors Java Interface Java static keyword Java this keyword

Java Break and Continue Statement

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.

Java Break Statement

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;

In Java, a break statement is majorly used for:

1. To exit a loop.

2. Terminate a sequence in a switch statement.

Using break to exit a loop

Example: with for 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

Example: with while 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

Example: Using break to terminate a sequence in a switch statement.

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

Java continue Statement

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.

Example: with for loop

// 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

Example: with while loop

// 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 vs continue

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.