The break statement is a control statement which is used to jump out of the loop.
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.
for( initialization; condition; statement){
//Block of statements
if(condition){
break;
}
}
/**
* Program to use for loop break statement example in java.
* @author javawithease
*/
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);
}
}
1
2
3
4
5
About the author