TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Java Break

Java break statement with concepts and examples, java break statement with labeled for loop and difference between break and continue in java.

<< Back to JAVA

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.

We can use Java break statement in all types of loops such as for loop, while loop and do-while loop.

Syntax:

jump-statement;  
break; 

Flowchart of Break Statement

java break statement flowchart

Java Break Statement with Loop

Example:

BreakExample.java

//Java Program to demonstrate the use of break statement  
//inside the for loop.
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);
    }
}
}

Output:

1
2
3
4

Java Break Statement with Inner Loop

It breaks inner loop only if you use break statement inside the inner loop.

Example:

BreakExample2.java

//Java Program to illustrate the use of break statement  
//inside an inner loop 
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);  
	                }  
	        }  
}
}

Output:

1 1
1 2
1 3
2 1
3 1
3 2
3 3

Java Break Statement with Labeled For Loop

We can use break statement with a label. The feature is introduced since JDK 1.5. So, we can break any loop in Java now whether it is outer or inner loop.

Example:

BreakExample3.java

//Java Program to illustrate the use of continue statement
//with label inside an inner loop to break outer loop
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);  
	                }  
	        }  
}
}

Output:

1 1
1 2
1 3
2 1

Java Break Statement in while loop

Example:

BreakWhileExample.java

//Java Program to demonstrate the use of break statement
//inside the while loop.
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++;
    }
}
}

Output:

1
2
3
4

Java Break Statement in do-while loop

Example:

BreakDoWhileExample.java

//Java Program to demonstrate the use of break statement
//inside the Java do-while loop.
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);
}
}

Output:

1
2
3
4

Java Break Statement with Switch

To understand the example of break with switch statement, please visit here: Java Switch Statement.






Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf