Simple Break Statement in Java
Java Break Statement is used to alter the flow of program code.For example,to stop running of inner loop we can use break statement.
Break Statement Syntax
//condition to test
break
Code Example:
public class BreakStatement {
public static void main(String args[])
{
for(int i=0;i<=10;i++)
{
if(i==6)
{
break;
}
System.out.println(i);
}
}
Breaking Inner loop with Break Statement
Inner loop can be broken using Java Break Statement.
public class BreakInnerLoop {
public static void main(String args[])
{
for(int i=1;i<=3;i++){
for(int j=1;j<=3;j++){
if(i==2&&j==2){
break;
}
System.out.println(i+" "+j);
}
}
}
}
Output
run:
1 1
1 2
1 3
2 1
3 1
3 2
3 3
BUILD SUCCESSFUL (total time: 0 seconds)