JAVA programs

java program using break statement

In this program we see the use of break keyword in java, we can use the break keyword inside the loop and the switch statement, in below program we used in a loop,

When the break statement is written inside a loop, the loop is immediately terminated and control goes to the code below the loop.

In switch break is used to terminate particular case.

Program

class Breakstatement

{

    public static void main(String args[])

    {

        int intarray[]=new int[]{1,2,3,4,5};

        System.out.println("Using break keyword terminate loop when condition is satisfy");

        for(int i=0;i<intarray.length;i++)

        {

            if(intarray[i]==3)

            break;

        else

        System.out.println(intarray[i]);

        }

}

}

When Run The Program Following Output:

Using break keyword terminate loop when condition is satisfy

1

2

Description

After the condition if(intarray[i]==3) satisfy the loop will terminate due to break keyword, other iteration will skip, and code below the loop will execute.

Releted Programs

© 2017 codewmAll rights reserved.