Join Our 4-Week Free Gen AI Course with select Programs.

Request a callback

or Chat with us on

Jump Statements in Java: Understanding break, continue, & return

Basics of SQL
Basics of SQL
icon
12 Hrs. duration
icon
12 Modules
icon
2600+ Learners
logo
Start Learning

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.

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; } } } }

Output

Location iteration = 1 Location iteration = 2 Location iteration = 3 Location iteration = 4

Explanation

  • The following example, we have a for loop that starts with int i =1 and continues as long as i is 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 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 loop is exited prematurely, and the program continues with the source code with the code after the loop.

Using break  with a while loop

class Main{ public static void main(String args[]){ int i = 1 ; while(i<= 5){ System.out.println("Loop Iteration "+i); i++ ; if(i> 3){ break; } } } }

Output

Loop Iteration 1 Loop Iteration 2 Loop Iteration 3

Explanation

  • 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); } } }

Output

Loop Iteration =0 Loop Iteration =1 Loop Iteration =2 Loop Iteration =4 Loop Iteration =5
DevOps & Cloud Engineering
Internship Assurance
DevOps & Cloud Engineering

Return Statement in Java

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.

 

 

Also Read: Java Interview Questions and Answers

Conclusion

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
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.  
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.
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.
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.
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.

Deploying Applications Over the Cloud Using Jenkins

Prashant Kumar Dey

Prashant Kumar Dey

Associate Program Director - Hero Vired

Ex BMW | Google

19 October, 12:00 PM (IST)

Limited Seats Left

Book a Free Live Class

left dot patternright dot pattern

Programs tailored for your success

Popular

Management

Data Science

Finance

Technology

Future Tech

Upskill with expert articles

View all
Hero Vired logo
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.

Data Science

Accelerator Program in Business Analytics & Data Science

Integrated Program in Data Science, AI and ML

Accelerator Program in AI and Machine Learning

Advanced Certification Program in Data Science & Analytics

Technology

Certificate Program in Full Stack Development with Specialization for Web and Mobile

Certificate Program in DevOps and Cloud Engineering

Certificate Program in Application Development

Certificate Program in Cybersecurity Essentials & Risk Assessment

Finance

Integrated Program in Finance and Financial Technologies

Certificate Program in Financial Analysis, Valuation and Risk Management

Management

Certificate Program in Strategic Management and Business Essentials

Executive Program in Product Management

Certificate Program in Product Management

Certificate Program in Technology-enabled Sales

Future Tech

Certificate Program in Gaming & Esports

Certificate Program in Extended Reality (VR+AR)

Professional Diploma in UX Design

Blogs
Reviews
Events
In the News
About Us
Contact us
Learning Hub
18003093939     ·     hello@herovired.com     ·    Whatsapp
Privacy policy and Terms of use

© 2024 Hero Vired. All rights reserved