Break Statement in Java

Break Statement in Java
Break Statement in Java 1

Break statement is used to exit the loop.we can use break statement in for,do ,while , switch and may other looping statements .when the controller encounters break; ,the controller skips the further code and goes to next statement outside the loop.




Break Statement in Java 2

Working of break statement:

Break Statement in Java 3


import java.util.*;
class X2
         public static void main(String args[])
            Scanner s=new Scanner(;
            int i=0,x=0,sum=0;
           for (i=1;i<=5;i++)
 	         System.out.printf("enter %dst number ",i);
 	         if(x<0 || x==0)
             sum += x;
          System.out.printf("the total sum is %d",sum);


enter 1st number 10
enter 2st number 20
enter 3st number 30
enter 4st number 0
the total sum is 60

this example is for the addition of any five numbers. but if the user gives any negative number the loop will be stopped using the break statement.

Default image
Vishal Devxo
Vishal is a DevOps engineer and a Backend developer, he spends all his time for creating good tutorials with better visuals and blogging, developed some projects based on Python-Django, some hacking modules and scripts in python
Leave a Reply