Jump Statements

You can also control the flow of the loop inside the body of any of the iteration statements by using break and continue. break quits the loop without executing the rest of the statements in the loop. continue stops the execution of the current iteration and goes back to the beginning of the loop to begin the next iteration.

1.Break

wherever compiler encounters break, the control will be thrown out of the loop. Example :

public class BreakExample {
	public static void main(String[] args) {
		for (int i = 0; i < 15; i++) {
			if (i == 5) {
				break; // after i equal to 5, below statements will not be
						// executed and the control will be thrown out of loop
			}
			System.out.println("i is " + i);
		}
	}
}

2.Continue

after encountering continue, the statement after continue will not be executed, whereas the loop continue as usual. Example :

public class ContinueExample {
	public static void main(String[] args) {
		for (int i = 0; i < 15; i++) {
			if (i == 5) {
				continue; // statements after this will be skipped if i==5 will
							// be true
			}
			System.out.println("i is " + i); // will not be printed when i
												// equals to 5
		}
	}
}

3.Break in the form of Goto

public class BreakLabel {
	public static void main(String[] args) {
		int i;
		for (i = 1; i < 4; i++) {
			one: {
				two: {
						System.out.println("\ni is " + i);
						if (i == 1)
							break one;
						if (i == 2)
							break two;
						// this is never reached
						System.out.println("won't print");
					}
					System.out.println("After block three.");
				}
				System.out.println("After block two.");
			}
			System.out.println("After block one.");
	}
}