break statement


Java break keyword is used to bring the program control out of the current loop.

So, when a break statement is encountered inside a loop, the loop is immediately terminated and the program control passes to the line immediately following the loop.

Note: In case of inner loop, it breaks only from the inner loop and program continues to run in outer loop.


Syntax:

break;

Example:

public class BreakExample
{
    public static void main (String[] args)
    {
        for(int i = 1; i < 5; i++)
        {
            if(i == 4)
            {
                break;
            }
        System.out.println(i);
        }
    }
}

Output:

1
2
3