break and continue statement in Java

break statement

The break statement is used to break or exit the while, do-while, for, for-each loops and switch statements.

Example

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

Output

2
4
6
8

continue statement

The continue statement is used skip the remaining statements in the current iteration and then start with the next iteration.

public class BreakEg
{
	public static void main(String args[])
	{
		for(int i=1; i<=10; i++)
		{
			if(i==5)
				continue;
			System.out.println(2*i);
		}
	}
}

Output

2
4
6
8
12
14
16
18
20