The perfect place for easy learning...

Java Programming

×

Topics List


Java Jump Statements





The java programming language supports jump statements that used to transfer execution control from one line to another line. The java programming language provides the following jump statements.

  • break statement
  • continue statement
  • labelled break and continue statements
  • return statement

break statement in java

The break statement in java is used to terminate a switch or looping statement. That means the break statement is used to come out of a switch statement and a looping statement like while, do-while, for, and for-each.

🔔 Using the break statement outside the switch or loop statement is not allowed.


The floowing picture depictes the execution flow of the break statement.

break statement in java

Let's look at the following example java code.

Java Program
public class JavaBreakStatement {

	public static void main(String[] args) {

		int list[] = {10, 20, 30, 40, 50};
		
		for(int i : list) {
			if(i == 30)
				break;
			System.out.println(i);
		}
		
	}

}

When we run this code, it produce the following output.

break statement in java

continue statement in java

The continue statement is used to move the execution control to the beginning of the looping statement. When the continue statement is encountered in a looping statement, the execution control skips the rest of the statements in the looping block and directly jumps to the beginning of the loop. The continue statement can be used with looping statements like while, do-while, for, and for-each.

When we use continue statement with while and do-while statements, the execution control directly jumps to the condition. When we use continue statement with for statement the execution control directly jumps to the modification portion (increment/decrement/any modification) of the for loop. The continue statement flow of execution is as shown in the following figure.

continue statement in java

Let's look at the following example java code.

Java Program
public class JavaContinueStatement {

	public static void main(String[] args) {

		int list[] = {10, 20, 30, 40, 50};
		
		for(int i : list) {
			if(i == 30)
				continue;
			System.out.println(i);
		}
		
	}

}

When we run this code, it produce the following output.

continue in java

Labelled break and continue statement in java

The java programming langauge does not support goto statement, alternatively, the break and continue statements can be used with label.

The labelled break statement terminates the block with specified label. The labbeled contonue statement takes the execution control to the beginning of a loop with specified label.

Let's look at the following example java code.

Java Program
import java.util.Scanner;

public class JavaLabelledStatement {
	public static void main(String args[]) {

		Scanner read = new Scanner(System.in);

		reading: for (int i = 1; i <= 3; i++) {
			System.out.print("Enter a even number: ");
			int value = read.nextInt();

			verify: if (value % 2 == 0) {
				System.out.println("\nYou won!!!");
				System.out.println("Your score is " + i*10 + " out of 30.");
				break reading;
			} else {
				System.out.println("\nSorry try again!!!");
				System.out.println("You let with " + (3-i) + " more options...");				
				continue reading;
			}
		}
	}
}

When we run this code, it produce the following output.

labbeled break and continue in java

return statement in java

In java, the return statement used to terminate a method with or without a value. The return statement takes the execution control to the calling function. That means the return statement transfer the execution control from called function to the calling function by carrying a value.

🔔 Java allows the use of return-statement with both, with and without return type methods.


In java, the return statement used with both methods with and without return type. In the case of a method with the return type, the return statement is mandatory, and it is optional for a method without return type.

When a return statement used with a return type, it carries a value of return type. But, when it is used without a return type, it does not carry any value. Instead, simply transfers the execution control.

Let's look at the following example java code.

Java Program
import java.util.Scanner;
public class JavaReturnStatementExample {
	
	int value;
	
	int readValue() {
		Scanner read = new Scanner(System.in);
		System.out.print("Enter any number: ");
		return this.value=read.nextInt();
	}
	
	void showValue(int value) {
		for(int i = 0; i <= value; i++) {
			if(i == 5)
				return;
			System.out.println(i);
		}
	}

	public static void main(String[] args) {

		JavaReturnStatementExample obj = new JavaReturnStatementExample();
		
		obj.showValue(obj.readValue());

	}
}

When we run this code, it produce the following output.

return in java