The break statement is a control statement which is used to jump out of the loop.
Types of break statement:
1. Unlabeled Break statement: is used to jump out of the loop when specific condition returns true. 2. Labeled Break Statement: is used to jump out of the specific loop based on the label when specific condition returns true.
Syntax of Unlabeled Break statement:
for( initialization; condition; statement){//Block of statementsif(condition){break;}}
Program to use for loop break statement example in java
/**
* Program to use for loop break statement example in java.
* @author W3spoint
*/publicclass ForLoopBreakExample {staticvoid forLoopBreakTest(int num){//For loop testfor(int i=1; i<= num; i++){if(i==6){//Break the execution of for loop.break;}System.out.println(i);}}publicstaticvoid main(String args[]){//method call
forLoopBreakTest(10);}}
/**
* Program to use for loop break statement example in java.
* @author W3spoint
*/
public class ForLoopBreakExample {
static void forLoopBreakTest(int num){
//For loop test
for(int i=1; i<= num; i++){
if(i==6){
//Break the execution of for loop.
break;
}
System.out.println(i);
}
}
public static void main(String args[]){
//method call
forLoopBreakTest(10);
}
}