Break Statement Java

Break Statement Java

break statement used inside a loop, the loop is immediately terminated and the program control resumes at the next statement following the loop.

What is a break statement in Java?

The Java break statement is used to break loop or switch statement. It breaks the current flow of the program at specified condition.

Flow of break statement

break statement flow in c AndroWep-Tutorials

Syntax

jump-statement;    
break; 

Break Statement example with Loop

public class BreakExample {  
public static void main(String[] args) {  
    //using for loop  
    for(int i=1;i<=10;i++){  
        if(i==6){  
            //breaking the loop  
            break;  
        }  
        System.out.println(i);  
    }  
}  
}  

Output

1
2
3
4
5

Break Statement example in while loop

public class BreakWhileExample {  
public static void main(String[] args) {  
    //while loop  
    int i=1;  
    while(i<=10){  
        if(i==6){  
            //using break statement  
            i++;  
            break;//it will break the loop  
        }  
        System.out.println(i);  
        i++;  
    }  
}  
}  

Output

1
2
3
4
Operators Java
Data Types Java
Variable Java
Second Short Program Java
Install and Setup Path Java
Hello World Java