In Java, jump statements control the flow of execution in a program by allowing some to skip sections of source code or terminate loops and methods. The primary jump statements in Java are break, continue, and return. Each statement serves a unique purpose and can significantly affect your code’s execution. This article will explore the jump statement in detail, providing examples to illustrate its usage.
What is a jump statement in Java?
A jump statement in Java is a control statement that allows execution flow to jump to a specific point in the program, interrupting the normal sequential execution. The main jump statements in Java, break, continue, and return, are used to exit loops, skip iterations, or return from methods.
Get curriculum highlights, career paths, industry insights and accelerate your technology journey.
Download brochure
Types of jump Statements in Java
There are four types of Jump statements in C language.
Break Statement
Continue Statement
Return Statement
Break Statement in Java
The break statement exits a loop or switch statement before its natural termination. When break is encountered, the control jumps out of the loop or switch block and moves to the statement immediately following.
Syntax
break ;
Using break with a for Loop
class Main{
public static void main(String args[]){
for(int i = 1 ;i<=5 ;i++){
System.out.println("Location iteration = "+i);
if(i > 3){
break;
}
}
}
}
In this example, we have a for loop that starts with int i =1 and continues as long as less than or equal to 5 (i <=5).
The loop will iterate from i = 1 to i =5 and during each iteration, it prints the current value of i using the System.out.println(“Loop Iteration” +i).
We have an if statement inside the loop that checks if i > 3. If the condition is true, the break statement is executed.
The result is that the loop is exited prematurely, and the program will continue the source code in the loop.
continue Statement
The continue statement in Java is a control flow statement used within loops. It allows you to skip the current iteration of a loop and proceed to the next iteration. The continue statement can be used in for, while and do-white loops.
Syntax
continue ;
continue with a for loop.
Program
public class Main{
public static void main(String args[]){
for(int i = 0;i<=5 ;i++){
if(i== 3){
continue ;
}
System.out.println("Loop Iteration ="+i);
}
}
}
Using return statement in Java: A return statement is used in Java to mark the end of a method and go back to its caller’s place. It can also provide a value to the method when the method has a particular return type. That makes the return statement compulsory for methods in Java if you want to return some value or a result from a method.
Using return without a value:
The following program demonstrates the return statement without a value. It is used to exit the method without returning any value.
Program
public class Main{
public static void Hellow(){
System.out.println("Harry Potter");
}
public static void main(String[] args) {
Hellow() ;
}
}
Output
Harry Potter
Using return with a value
In this section, the return type other than void, such as int, doubly, String, or any other data type, the return statement is used to exit the method and return a value of the specified type.
Program
public class Main{
public static int addition(int x, int y){
return x + y ;
}
public static void main(String[] args) {
int output = addition(20,30) ;
System.out.println(output);
}
}
Output
50
Advantages and Disadvantages of Jump Statements in Java
Let’s discuss the advantages and disadvantages of Jump Statement in Java language:
Jump Statement
Advantages
Disadvantages
break
It allows immediate exit from loops or switch blocks, improving control flow and simplifying code by preventing unnecessary iterations.
If overused or misused, it can make the program harder to read and disrupt the logical flow of nested loops, reducing clarity.
continue
It skips the current iterations and moves to the next iterations, making it useful for specific conditions inside the loops and enhancing flexibility.
It makes code less effective if the reader does not clearly understand it.
Overuse may lead to less structured loop management.
return
It efficiently exits methods, transferring control back to the caller. Reducing complex conditions in methods can improve source code readability.
Excessive use may lead to premature exits, making debugging and source code flow harder to follow. Using multiple return points in a method can also reduce maintainability.
The jump statements in Java: they are the break, statement, continue and return. They are essential in managing the control flow, especially in instances such as loops, switches and methods. The break statements are used to terminate a loop or a switch block altogether and thus, no further execution will take place in the current environment. The other is the continue statement, which provides more control to the loop by letting it go directly to the next iteration without completing the current one. The return statement used within methods allows the method to be excited and, if required, to pass back a specific value to the method that is called. These statements give Java programs improved modulation and readability and afford fine-grained control over how a program is to run. Enrol in Hero Vired’s Certificate Program in Application Development to explore Java in detail.
FAQs
What is the purpose of the break statement in Java?
The break statement is used to prematurely exit a loop (for, while, or do-white) or switch block, stopping further iterations or execution within the block.
Can break be used outside of loops or switch statements?
No, break statements can only be used inside loops(for, while, or do-white) and switch statements. Using it outside of these will result in a compilation error.
How does the return statement differ from the break and continue?
The return statement is used to exit a method and optionally return a value while breaking and continuing to control the flow within loops or switch cases.
Can you use return in a loop?
Yes, return can be used within a loop. If a return statement is encountered in a loop, it will terminate the entire method, including the loop. Return controls the calling method.
What happens if you use a break as a method?
The break statement cannot be directly inside a method unless inside a loop or switch case. If used outside there, it will cause a compilation error.
Hero Vired is a leading LearnTech company dedicated to offering cutting-edge programs in collaboration with top-tier global institutions. As part of the esteemed Hero Group, we are committed to revolutionizing the skill development landscape in India. Our programs, delivered by industry experts, are designed to empower professionals and students with the skills they need to thrive in today’s competitive job market.