Home >>Java Tutorial >Java Break

Java Break Statement

Java Break Statement

The loop in the Java language is terminated straight away and the program control resumes itself at the next statement following the loop whenever a break statement in Java is encountered inside a loop. The break is generally used to break the loop or switch statement in Java. The current flow of the program that is at a specified condition is broken by it. Whenever there is a case of inner loop then it only breaks the inner loop. Java break statement can be used by the programmers in all types of loops like do-while loop, for loop, and while loop.

Syntax

Here is the syntax of the break statement in Java is depicted below:

jump-statement;    
break;   

Java Break Statement with Loop

Here is an example of the break statement with the loop that will depict the working of it:

public class BreakEx{  
public static void main(String[] args) 
{  
    for(int i=1;i<=10;i++)
	{  
        if(i==4)
		{  
            //when condition match it will break the loop  
            break;  
        }  
        System.out.println(i);  
    }  
}  
}  
Output :
1
2
3

Java Break Statement with Nested(Inner) Loop

If the break statement is used inside of a loop then it will only breaks the inner loop. Here is an example that is depicting the same:

public class BreakEx2{  
public static void main(String[] args)
{  
	//Here is Outer loop
	for(int i=1;i<=2;i++)
	{    
			//Here is inner loop  
			for(int j=1;j<=2;j++)
			{    
				if(i==1 && j==1)
				{    
					//break the statement when condition match  
					break;    
				}    
				System.out.println(i+" "+j);    
			}    
	}    
}  
}  
Output :
2 1
2 2

Java Break Statement with Labeled For Loop

The break statement can be used with a label by the programmers. This label feature was introduced by the JDK 1.5 version of Java. By the use of this function, now the programmers can basically break any loop in Java now whether it is an outer loop or an inner. Here is an example that will make you understand it better:

public class BreakEx3{  
public static void main(String[] args) {  
            xy:  
            for(int i=1;i<=2;i++)
			{    
                    xz:  
                    for(int j=1;j<=2;j++)
					{    
                        if(i==2&&j==2)
						{    
                            //use break  
                            break xy;    
                        }    
                        System.out.println(i+" "+j);    
                    }    
            }    
}  
}
Output :
1 1
1 2
2 1

Java Break Statement in while loop

Here is an example of the break statement in while loop:

public class BreakEx4{  
public static void main(String[] args) 
{  
    int i=1;  
    while(i<=15)
	{  
        if(i==3)
		{  
            //match the condition and  break the  statement  
            i++;  
            break;
        }  
        System.out.println(i);  
        i++;  
    }  
}  
}  
Output :
1
2

Java Break Statement in do-while loop

Here is an example of the break statement in do-while loop:

public class BreakEx5{  
public static void main(String[] args)
{  
    int i=1;  
    do{  
        if(i==3)
		{  
           i++;  
           break; 
        }  
        System.out.println(i);  
        i++;  
    }
	while(i<=15);  
}  
}  
Output :
1
2