Java – Break Statement Example

Java supports break statement as other language support. Break statement in java is used to exit from running loop program on pre defined condition. Break statement can be implement with for loop while loop statement. If two loops is nested break statement will terminate inner loop and outer loop will continue to work. We have to use break with if condition to break it from loop.

Java break example explains how to use break statement in java.

public class JavaBreakStatementExample {

    public static void main(String[] args) {

        for(int i=0;i<=20;i++)
        {
            if(i==10)
            {
                break;
            }
            System.out.println("Break loop :"+i);
        }
    }
}

Output

Break loop :0
Break loop :1
Break loop :2
Break loop :3
Break loop :4
Break loop :5
Break loop :6
Break loop :7
Break loop :8
Break loop :9

In above example, we are using break with for loop. This loop is supposed to execute 20 times. We are using if condition to check loop, if it reach on 10 value of i. it will execute break statement. Once break is executed it will terminate for loop and control will come transfer out of for loop.

Tags:

Bookmark  

 

Leave a Reply

Security Code:

 

  Random Post