Jump Statement in Java: Break, Continue and Return

Do you know about the “Jump statement in Java“? Understanding the jump statements is a crucial step in mastering Java! This article is all about Break, Continue, and Return statements, that will help you in increasing the efficiency and flexibility of your code. 

If you encounter any challenges while going through this article and require help with your Java Assignment, you have the option to enlist the services of skilled programmers at CodingZap. They are experts in the field and ready to assist you.

Let us try to find out some essential facts & details about the jump statements in Java programming languages. So, we will start our discussion with the very basic details.

Summary of the Article:

  • Jump or jumping statements in Java are those control statements that transfer the flow of a program from one part to another.
  • There are mainly three jump statements in Java – break, continue, and return. 
  • Jump statements are important as they help us to determine the execution control so that our program can run smoothly.
  • These are generally used to come out of or end the execution of a loop or switch case.
  • Each jump statement has a different way of transferring control from one block of the program to another.

What Are Jump Statements In Java?

The Java jump statements are considered as one of the special syntax or the set of statements that has the power to skip some lines inside of the code, for the sake of solving an issue. In simple words, the Java jump statements are utilized for jumping some lines of code. 

Well, there are many statements in Java and one of them is the if else statement. If else statement in Java has many benefits. Like, when there are two or more paths available, then we need to use the if else statement.

flowchart

The above flowchart helps to analyze the jump statements in Java. While encountering any of the jump statements in Java, the flow will take a different path or branch to get executed. Hence, the jump statements are often termed “Branching Statements”.

What Are The Types Of Jump Statements In Java and How To Implement Them?

There are mainly three major variations present for Jump statements or branching statements in Java programming languages. Each one of them is used for different purposes. The types of Jump statements are the following:

  1. Break Statement
  2. Continue Statement
  3. Return Statement

We will know all about them one by one briefly. 

  • How To Implement The Break Statement In Java?

In Java programming language, the break statement is used to terminate any loop statement, especially an infinite loop. Whenever the compiler encounters one break statement, it will terminate the existing loop & come out of it.

Break statement in Java

Consider an example where you are given an array that consists of 8 values. You need to print the index of this array starting from index 1. You are required to stop printing and exit the program as soon as you reach the 5th index of the array.

Therefore, your output will contain indexes 1,2,3 and 4. But how can you do it? Leaving the loop running till the last index will lead to time inefficiency. 

In this case, you can make use of break for exiting a loop when you get the desired result. The general syntax for it is given below.

Syntax: break;

The syntax is simple enough. Only one should need to write a break statement inside of the program. 

String is also an interesting concept in Java, and if you want to learn about comparing strings in Java then you can check out our article.

Let us have a look to see how can we implement it using the code given below.

Code To Demonstrate The Use Of Break Statement:
				
					public class Main
{
	public static void main(String[] args) {
System.out.println("Printing Started Untill Encounter Of Break: ");
int n= 8;
for (int i = 1; i < n; i++) { // Starting A For Loop
if (i == 5){ //Condition To Implement Break Statements
                System.out.println("--Encountered Break--");
                break; // Break Statement
            }
System.out.println(i); // Printing Data
        }
	}
}

				
			

Steps Of The Program:

  1. Here, the public static void main() is used as an entry point for the code.
  2. At first, one for-loop will be implemented. Along with some conditions provided there.
  3. Now, one separate condition will be implemented inside the loop. If the condition is fulfilled, the break statement will be executed.
  4. In all other cases, the for loop will print the elements individually. We will normally print the data.

Output:

Break Statement Output

From the above output, we can see that the break jump statements were executed properly. Before fulfilling the condition, all the elements were getting printed.

When the condition is matched, the inner statement will be printed & break is executed. No other remaining statements are executed. So, the compiler gets out of the loop. As there is no next statement present, the program ends.

  • How To Implement Break Statement In Java As Goto Statement?

The Java programming language doesn’t use the goto statement as a jump statement. Instead, it makes use of labels as a form of goto for unconditional jump. So, how to use the goto statement in Java?

The break statement can be implemented as like goto statement. You will find some similarities with the Switch statement in programming languages. Once, the syntax will be encountered, it will be jumped to a specific point inside of the program. 

In the switch case condition, if some value gets matched, then some operation will be done. The same thing will be executed here also. If one condition is fulfilled, then the program control will be shifted to another line in the program. 

General Syntax: break label; 

Code To Demonstrate The Use Of Break Statement As Goto or Switch Statement:
				
					public class Main
{
	public static void main(String[] args) {
	    for (int a = 1; a <= 2; a++) {
        first: { // Creating First Break
        second: { // Creating Second Break
            System.out.println();
            System.out.println("Data Number: " + a); // Printing Data Number
            
            if (a == 1) // Condition For First Lebel
                break first; // Break Statement
            if (a == 2) // Condition For Second Lebel
                break second; // Break Statement
        } // End Of Second Lebel
            System.out.println("Executed Second Lebel");
        } // End Of First Lebel
            System.out.println("Executed First Lebel");
        }
}
}

				
			

Steps Of The Program:  

  1. First, a normal for loop will be declared where the number of iterations will be specific. The number of switch case, you want to declare, so many iterations it should have.
  2. The public static void main() is used as an entry point for the code.
  3. Now, the break label will be declared. We have marked the loop will execute for two iterations. So, there will be two labeled break statements inside of the existing loop. Declare it as we have done.
  4. The labeled break statement will be declared along with an open brace. Inside of that, we will declare some conditions. Under those conditions, the labels will be described.
  5. We will declare the next statement randomly & close the labeled break statement inside the program.
  6. The resulting output is printed using System.out.println(); for each case.

Output:

Switch Case Statement Output

From the above output, we can notice that encountering a certain value, the particular statement meant for it was printed.

In the first case, as there was only one statement left after closing the labeled break statement, it was printed. The same was done for the second condition.  

  • How To Implement Continue Statement In Java?

The continue statement is one of the jump statements in Java programs. The continue statement works completely different format. It doesn’t end the loop execution. Rather, it helps to skip the iteration of the loop.

Continue Statement In Java Flowchart

Suppose, one continue statement is encountered inside of one loop. In that case, it will go back to the start of the loop & execute the next iteration of the loop. Whatever statements are present after the continue statements will not execute.

Let us understand how to use the continue statement with the help of an example. Consider an example where we are iterating through an array. The for loop starts from 1 and continues up to 5. Let’s add a condition where when the loop iterates a third time, we skip that iteration and move to other iteration statements.. 

One of the common scenarios where you can use the continue statement is when you need to print or iterate over a specific set of values. For instance, printing the percentage of students in a class who have even roll numbers. 

Now, let us see a code example to see the program execution when we include continue in our code.

Code To Demonstrate The Use Of Continue Statement:
				
					public class Main
{
	public static void main(String[] args) {
	    System.out.println("Printing Started: ");
        for (int i = 1; i <= 5; i++) { // Starting A For Loop
            if (i == 3){ // Condition To Implement Continue
                System.out.println("--Encountered Continue--");
                continue; // Continue Implemented
            }
            System.out.println(i); // Printing Data
        }
	}
}

				
			

Steps Of The Program:

  1. One loop will be implemented first with some random conditions & starting index numbers.
  2. The role of this loop will be to print the elements number one by one till the condition is satisfied. In the meantime, we have declared one condition that will work as the control statements.
  3. Upon fulfilling the condition, the continued statement will be executed. But before that one print statement will also work.
  4. Outside of that condition, the statement for printing the data should be placed.

Output:

Continue Statement Output

The output shows that starting the current loop was printing each & every data. When the continue statement was encountered, the existing statement was present before that also, but the element data was not printed. 

This is because the continue statement transfers the compiler to the next iteration of the for loop. So, from that other values are printed unless the loop gets finished automatically.  

Now, when someone asks you, “Name two jump statements in Java.” You can answer by saying continue and break statements. 

Let us also get to know about the return statement in Java. Read further to know!

  • How To Implement Return Statements in Java?

Oftentimes, the return statement is not considered as the jump statement in Java programs. This is because, in this case, the return statement will not transfer the control to any other statement, rather it transfers control to a method.

Return Statement In Java Flowchart

A method should be called from the main method or any other method. In that case, the return statement can be implemented inside of that method. The calling method is necessary for this jump statement to execute it. No looping statements are involved here.

Suppose you want to find out the sum of two integers using a new method. You have to print the sum in that method as well as the main method. A return statement will help you with it. Let’s see an example to know how.

Code To Demonstrate The Use Of Return Statement:
				
					public class Main
{
	public static int sum_of_integers(int n1, int n2) { // Creating Another Method
	    int sum = n1+n2;
	    System.out.println("Printing In New Method");
	    System.out.println("The sum of integers is:" + sum);
	    return sum; // Returning Data
	}
	
	public static void main(String[] args) {
           int main_sum = sum_of_integers(5, 7); // Calling Method
            System.out.println();
            System.out.println("Printing In Main Method");
            System.out.println("The sum of integers is: " + main_sum);
        }
	}



				
			

Steps Of The Program:

  1. First, the second method will be declared other than the main method in the program.
  2. This method will take two integers n1 and n2 as parameters and calculate their sum. 
  3. System.out.println(“Printing In New Method”); depicts that this method has been called or executed.
  4. Inside the main function, that method will be called. After calling the method, another statement declared to print it as the ending statement.
  5. At last, we can print the sum that is stored in main_sum variable. The function call returns the addition value that is stored here. 

 Output:

Return Output

From the above output, we can see that the statement inside of the method declared is first. After that, the statement inside the main method is printed. So, the control first goes to the new method & after that to the main method.

Hence, we can see that the sum of the given two numbers i.e. 5 and 7 is calculated and printed with the help of the statements inside the function and also in the main method with the help of the return statement.

Best Practices For Using Jump Statement In Java

Using jump statements in Java can add clarity to your code and help in better program execution. You should keep some points or guidelines in mind when you work with the jump statements.

  • Ensure iteration control in your Java code by using break statements to efficiently exit the loops or switch cases.
  • Document the reason for using each jump statement in your code to provide a clear purpose when you revisit the code.
  • Make sure you use clear variable names for labels to enhance code readability.
  • Using jump statements in deep or complex nested loops is inadvisable as they can result in hindrances in debugging.

Conclusion:

As we saw, it is very important to know jump statements in Java programming language. The jump statements are declared to get easy control of the program. Sometimes, you might find a piece of code should be executed inside of the program depending upon some cases. Jump statements help to do that by declaring the condition to it. 

It is advisable to clear the basic Java programming skills before moving to this topic. We have witnessed that much basic information has been used on this topic. Clearing those topics before moving ahead will help you to grab the concept more easily.

Key Takeaways:

  • Jump statements in Java are used to facilitate a seamless flow of control when we need to transfer the control from one part to another in the program.
  • Break can be used to exit loops or switch cases, and continue statements can be used to skip a certain iteration.
  • You can use the return statement to terminate method execution and for returning values.
  • Following best practices and guidelines related to these jump statements can help you write better, clearer, and more readable Java code.

Get Programming Help Now

Leave a Comment

Your email address will not be published. Required fields are marked *