Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

The break statement

Java Programming Language break Statement

The statement that is used to break/stop the flow of loops & switch statement is known as break statement.

Inside the loop, break statement terminates the loop execution when it is encountered.

Inside switch statement, break statement is used to jump out of the switch statement instantly when it is encountered.

Syntax

break;

Working

Java Programming Language break Statement Working in for, while and do-while loops

After terminating a loop by using the break statement, program control is transferred immediately to the statement after the loop.

Example No. 1

 class BreakDemo1 
 {
     public static void main(String args[])
     {
         int x;
         for(x=11;x<=20;x++)
         {
             if(x%5==0)
                  break;
			
             System.out.println(x);
         }
         System.out.println("Outside loop");
     }
 }

Output

11
12
13
14
Outside loop

Example No. 2

class BreakDemo2 
{
    public static void main(String args[])
    {
        int x;
        x=11;
        while(x<=20)
        {
            if(x%5==0)
                  break;
			
            System.out.println(x);
            x++;
        }
        System.out.println("Outside loop");
    }
}

Output

11
12
13
14
Outside loop

 

Leave a reply