The break statement terminates the execution of the loop in which it is defined and the control is transferred immediately to the next executable statement after the loop. The break statement is normally used with either while, do-while, for or a switch statement. It is mostly used to exit early from the loop by skipping the remaining statements of loop or switch control structures.
It is simply written as
break;
//Program to Show Importance of Break Statement
public class ImpBreak
{
public static void main(String[] args)
{
System.out.println("Show Importance of Break Statement");
for(int i=1; i<=10;i++)
{
System.out.println("i = "+i);
if(i==5)
{
System.out.println("\nBye");
break;
}
}
}
}
In this example, the for loop executed starting from i=1 to 10 in steps of 1. Now when the condition (i==s)in the body of the loop is satisfied, the break statement causes the control to move out of for loop