Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
The execution of a Javaprogram is done in sequential order. Looping statements like for, while, and do-while are used to execute a set of statements repeatedly. If such an execution were to continue, it would go on forever. This can be prevented by adding appropriate terminating conditions to the loop or by implementing jump statements.
A jump statement in Java helps transfer the control of a program from one line of the code to another and skip the ones in between.
Jumping statements are control statements that move program execution from one location to another. Jump statements are used to shift program control unconditionally from one location to another within the program. Jump statements are typically used to abruptly end a loop or switch-case.
Types of Jump Statements in Java
Break Statement
Continue Statement
Return Statement
1. Break Statement
Break statements in Java is used in three ways:
Using Break Statement to exit a loop
In Java, the break statement is used to exit loops prematurely, similar to how it's used in other programming languages. You can use it within for, while, and do-while loops to stop the loop's execution when a specific condition is met. The syntax for using the break statement in Java is as follows:
while loop:
java
java
while (condition) {
// Code inside the loop
if (condition) {
break;
// break keyword
}
// Code continues inside the loop
}
// Code after the loop
You can also try this code with Online Java Compiler
for (int i = 0; i < limit; i++) {
// Code inside the loop
if (condition) {
break;
// break keyword
}
// Code continues inside the loop
}
// Code after the loop
You can also try this code with Online Java Compiler
In Java, the break statement is not directly used as a form of goto. The break statement is specifically designed to exit from loops prematurely, and it cannot be used to jump to arbitrary locations in the code like a traditional goto statement.
The goto statement is unavailable in Java because it can lead to confusing and hard-to-maintain code. Instead, Java encourages using structured programming constructs like loops, conditional statements, and methods to control the flow of the program.
java
java
boolean conditionMatch = false;
while (Condition) {
// Code inside the loop
if (conditionToBreak) {
conditionMatch = true;
break;
}
// Code continues inside the loop
}
// Code after the loop
if (conditionMatch ) {
// Code to be executed after the "goto" point
}
You can also try this code with Online Java Compiler
In Java, we can use the break statement within a switch case to exit the switch block and prevent fall-through to subsequent cases. Here's an example:
Java
Java
class Main { public static void main(String[] args) { int day = 3;
switch (day) { case 1: System.out.println("Monday"); break; // Exit switch after executing case 1 case 2: System.out.println("Tuesday"); break; // Exit switch after executing case 2 case 3: System.out.println("Wednesday"); break; // Exit switch after executing case 3 case 4: System.out.println("Thursday"); break; // Exit switch after executing case 4 case 5: System.out.println("Friday"); break; // Exit switch after executing case 5 default: System.out.println("Weekend"); break; // Exit switch for any other value } } }
You can also try this code with Online Java Compiler
In this example, the break statement is used after each case to exit the switch block. This prevents the flow from falling through to subsequent cases. If day is, for example, 3, only "Wednesday" will be printed, and the switch statement will terminate after that case.
2. Continue Statement
Continue is a jump statement used when a loop’s current iteration is to be skipped. It allows the control to temporarily exit the loop and ignore the remaining statements that follow. After this jump, the control immediately moves with the next iteration of the same loop.
java
java
public class Main
{
public static void main(String args[])
{
for (int i = 10; i > 0 ;i--)
{
if (i%2 == 0)
continue;
System.out.print(i+ " ");
}
}
}
You can also try this code with Online Java Compiler
This Java code prints odd numbers in reverse order from 9 to 1 using a for loop. The continue statement skips even numbers.
3. Return Statement
The return keyword is used to transfer a program’s control from a method back to the one that called it. Since the control jumps from one part of the program to another, return is also a jump statement.
It can be in Two forms:
1. Return with a value
return expression; Where expression is the value or result that the function is returning.
2. Return without a value
return; It is without an accompanying expression.
java
java
public class Main
{
public static int add(int a, int b){
int sum = a+b;
return sum;
}
public static void main(String[] args) {
int x = 5, y = 10;
int sum = add(x, y);
System.out.println("Sum of a and b: " + sum);
}
}
You can also try this code with Online Java Compiler
This Java code defines a program with a function add() that calculates the sum of two integers. In the main method, it initializes x and y, calls add, and prints the sum.
Java program to show the working of break, continue and return statement
java
java
public class BreakContinueReturnDemo {
public static void main(String[] args) {
// Using break statement in a loop
System.out.println("Using break statement in a loop:");
for (int i = 1; i <= 5; i++) {
if (i == 3) {
break;
}
System.out.println("Current value: " + i);
}
// Using continue statement in a loop
System.out.println("\nUsing continue statement in a loop:");
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue;
}
System.out.println("Current value: " + i);
}
// Using return statement in a method
System.out.println("\nUsing return statement in a method:");
int result = addNumbers(5, 10);
System.out.println("Result of addition: " + result);
}
// Method to demonstrate the use of return statement
public static int addNumbers(int a, int b) {
int sum = a + b;
return sum;
// Code after the return statement won't be executed
}
}
You can also try this code with Online Java Compiler
Using break statement in a loop:
Current value: 1
Current value: 2
Using continue statement in a loop:
Current value: 1
Current value: 2
Current value: 4
Current value: 5
Using return statement in a method:
Result of addition: 15
Why Are Jump Statements Important in Java Programming?
Controlling the flow of execution is essential in Java programming, especially within loops and methods. Jump statements like break, continue, and return give developers precise control over execution, allowing them to exit, skip, or return early—making code more efficient, readable, and modular.
break – Exiting Loops or Switch Cases Early
The break statement immediately ends the nearest loop or switch block. It's useful when a desired outcome is found before completing all iterations, saving unnecessary computation.
Example:
for (String name : students) {
if (name.equals("Radha")) {
System.out.println("Found!");
break; // Stop searching after finding the name
}
}
Why it matters: It improves performance by stopping execution early—like hanging up a phone call once you get the answer.
continue – Skipping the Current Iteration
The continue statement skips the rest of the current loop iteration and proceeds to the next one. It’s helpful for skipping invalid, unwanted, or already-processed data.
Example:
for (String input : inputs) {
if (input.isEmpty()) continue; // Skip empty entries
process(input);
}
Why it matters: It keeps loops clean and focused—like skipping an empty row while reading a spreadsheet.
return – Exiting a Method Early
The return statement ends method execution and optionally returns a value. It’s vital for exiting early when a specific condition is met, improving modularity and clarity.
Example:
public boolean isValid(String email) {
if (email == null || !email.contains("@")) return false;
return true;
}
Why it matters: It avoids unnecessary checks—like ending a form validation once you spot a missing required field.
Comparison with Other Control Flow Statements
Java offers various control flow mechanisms. Jump statements (break, continue, return) control how execution moves, often abruptly. In contrast, conditional statements (if, else, switch) guide what logic to execute based on conditions.
Comparison Table
Parameters
Jump Statements (break, continue, return)
Conditional Statements (if, else, switch)
Purpose
Alter execution flow immediately
Direct logical flow based on conditions
Control Scope
Local (loops, methods)
Broad (any part of logic)
Readability
Can decrease if overused
Generally structured and predictable
Typical Use Case
Exit early, skip steps, return from methods
Choose between logic branches
Jump Statements
Jump statements introduce abrupt changes in flow. They're powerful in optimizing logic (like breaking a loop early) or improving modularity (with return), but excessive use—especially nested break or continue—can reduce clarity and make debugging harder.
Conditional Statements
Conditionals provide structured decision-making. They're generally easier to read and maintain since they follow a predictable, branching pattern. Well-designed conditionals can replace complex jump-based logic in many scenarios, improving overall code design.
Impact Analysis
While jump statements can increase performance by preventing unnecessary execution, too many of them can make the code harder to understand, especially in nested structures. In contrast, conditional statements promote clarity and maintainability, especially in larger applications or team-based development.
Real-World Use Cases
Understanding the real-world application of jump statements helps solidify when and why to use them in your Java programs. These examples show practical, relatable scenarios.
Using break in Searching Algorithms
Consider a scenario where you're searching for a specific item in a list:
for (String user : userList) {
if (user.equals("Narayan")) {
System.out.println("User found!");
break;
}
}
Using break here stops the loop once the item is found, saving resources. It’s ideal in search tasks where only the first match is needed, improving overall efficiency.
Skipping Invalid Input with continue
Suppose you're reading data inputs where some entries may be invalid or empty:
for (String entry : inputs) {
if (!entry.matches("\\d+")) continue; // Skip non-numeric inputs
processNumber(Integer.parseInt(entry));
}
Using continue lets you cleanly skip invalid data while keeping the rest of the loop logic focused and efficient. It’s commonly used in input filtering or data cleaning.
Using return in Validation and Utility Methods
In validation methods, return is useful for ending execution as soon as a failure is detected:
public boolean isEligible(int age) {
if (age < 18) return false;
return true;
}
This makes the method concise and avoids unnecessary logic after the early condition. It's widely used in utility or helper methods to make code modular and readable.
Frequently Asked Questions
Which is not a valid jump statement in Java?
Jump statements in Java: `break,` `continue,` and `goto` (unused). `return` is not a jump statement; it exits a method and returns a value to the caller.
Which jump statement is used to terminate the program?
In Java, the `System.exit()` method terminates the program. It is not a jump statement but a method that forces the program to exit with a specified status code.
What is the difference between Goto statement and break statement?
The goto statement is used to transfer control unconditionally to another labeled part of the program, while the break statement is used to exit from loops or switch statements prematurely based on a certain condition.
What happens when a break statement is used inside a nested loop?
A break statement inside a nested loop terminates only the innermost loop in which it is placed, resuming execution after that loop. Outer loops continue execution unless explicitly interrupted.
What is the difference between break and return in Java?
break exits a loop or switch statement, resuming execution immediately after it. return exits the entire method, optionally passing a value to the caller, halting further method execution.
Conclusion
Jump statements in Java, such as break, continue, and return, are essential tools for controlling the flow of execution within loops and methods. They help manage program logic efficiently by allowing you to exit or skip parts of code based on specific conditions. Understanding how and when to use these statements can lead to cleaner, more readable, and optimized code. This article extensively discusses the break and continue statements in Java and their implementation with examples.