1. What are Jump Statements?
Jump statements are used to change the normal flow of program execution.
Normally, Java executes statements one by one. But sometimes, we need to:
- Stop a loop immediately.
- Skip the current iteration of a loop.
- Exit from a method.
- Return a value from a method.
Java provides three main jump statements:
breakcontinuereturn
2. Break Statement
The break statement is used to terminate a loop or a switch statement immediately.
When Java encounters break, it exits the nearest enclosing loop or switch.
Syntax
break;
Example: Break in a for loop
public class Main {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
break;
}
System.out.println(i);
}
}
}
Output
1
2
Explanation
- Initially,
i = 1. - The condition
i == 3is false. - Java prints
1. - The loop continues and prints
2. - When
i = 3, the condition becomes true. - The
breakstatement executes. - The loop terminates immediately.
Important: When break executes, the remaining iterations of the loop are skipped.
Break in a while loop
int i = 1;
while (i <= 5) {
if (i == 4) {
break;
}
System.out.println(i);
i++;
}
Output:
1
2
3
When i becomes 4, the loop stops.
Break in a switch statement
The break statement is also commonly used in switch statements.
int day = 2;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
default:
System.out.println("Invalid day");
}
Output:
Tuesday
Here, break prevents execution from continuing into the following cases.
3. Continue Statement
The continue statement is used to skip the current iteration of a loop.
Unlike break, it does not terminate the entire loop. Instead, execution moves to the next iteration.
Syntax
continue;
Example: Continue in a for loop
public class Main {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue;
}
System.out.println(i);
}
}
}
Output
1
2
4
5
Explanation
- When
i = 1, Java prints 1. - When
i = 2, Java prints 2. - When
i = 3, the condition becomes true. - The
continuestatement skips the remaining statements in that iteration. - The loop moves to the next iteration.
- Java prints 4 and 5.
Important: continue skips only the current iteration. The loop continues running.
Continue in a while loop
int i = 0;
while (i < 5) {
i++;
if (i == 3) {
continue;
}
System.out.println(i);
}
Output:
1
2
4
5
Here, when i becomes 3, the continue statement skips the print statement.
Note: In a while loop, make sure the loop variable is updated before continue when necessary. Otherwise, the loop may become infinite.
4. Difference Between Break and Continue
Both statements change the normal execution of loops, but their purposes are different.
| Break | Continue |
|---|---|
| Terminates the entire loop. | Skips the current iteration. |
| Control moves outside the loop. | Control moves to the next iteration. |
| Remaining iterations are not executed. | Remaining iterations continue normally. |
| Can be used in loops and switch statements. | Used in loops. |
Simple Example
Using break:
for (int i = 1; i <= 5; i++) {
if (i == 3) {
break;
}
System.out.println(i);
}
Output:
1
2
Using continue:
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue;
}
System.out.println(i);
}
Output:
1
2
4
5
5. Return Statement
The return statement is used to exit from a method.
It can also send a value back to the place where the method was called.
There are two common uses of the return statement:
- Return without a value.
- Return with a value.
5.1 Return Without a Value
A method with the void return type does not return a value.
We can use return; to exit that method.
Example
public class Main {
static void display() {
System.out.println("Hello Java");
return;
// Method exits here.
}
public static void main(String[] args) {
display();
System.out.println("Welcome");
}
}
Output:
Hello Java
Welcome
Explanation
- The
main()method callsdisplay(). - The
display()method printsHello Java. - The
returnstatement exits thedisplay()method. - Execution continues from the place where
display()was called. - The
main()method printsWelcome.
Important: In a void method, return; is optional at the end of the method.
5.2 Return With a Value
A method can return a value to the calling method.
The return type of the method determines what type of value it should return.
Example
public class Main {
static int add() {
int a = 10;
int b = 20;
return a + b;
}
public static void main(String[] args) {
int result = add();
System.out.println(result);
}
}
Output:
30
Explanation
-
static int add()means the method returns an integer. -
return a + b;calculates the sum and sends the result back. -
int result = add();stores the returned value. -
System.out.println(result);prints 30.
Different Return Types
Java methods can return different types of values.
Integer return type:
static int getNumber() {
return 100;
}
String return type:
static String getName() {
return "Java";
}
Double return type:
static double getPrice() {
return 99.50;
}
Boolean return type:
static boolean isPassed() {
return true;
}
The returned value must be compatible with the method's declared return type.
6. Multiple Return Statements in a Method
A method can contain multiple return statements.
However, once one return statement executes, the method exits immediately.
Example
public class Main {
static int checkNumber() {
int number = 10;
if (number > 5) {
return 100;
}
return 200;
}
public static void main(String[] args) {
System.out.println(checkNumber());
}
}
Output:
100
Explanation
- The variable
numberstores 10. - Java checks whether
number > 5. - The condition is true.
-
return 100executes. - The method exits immediately.
-
return 200is never reached.
Important: Only the first reached return statement executes.
7. Difference Between Break and Return
| Break | Return |
|---|---|
| Exits a loop or switch. | Exits the current method. |
| Does not return a value. | Can return a value. |
| Execution continues after the loop or switch. | Execution continues from the method caller. |
| Used in loops and switch statements. | Used inside methods. |
Example
Break:
for (int i = 1; i <= 5; i++) {
if (i == 3) {
break;
}
System.out.println(i);
}
System.out.println("Loop completed");
Output:
1
2
Loop completed
Return:
static void display() {
System.out.println("Hello");
return;
// Method exits here.
}
Here, the display() method exits when return executes.
8. Important Points to Remember
- Java has three main jump statements:
break,continue, andreturn. -
breakterminates the nearest enclosing loop or switch. -
continueskips the current iteration and moves to the next iteration. -
returnexits the current method. - A
voidmethod does not return a value. - A method with a return type such as
int,String, ordoublemust return a compatible value on every possible path where it completes normally. - A method can contain multiple return statements.
- Once a
returnstatement executes, the method exits immediately. -
breakandcontinueare commonly used with looping statements. -
returnis commonly used when a method needs to send a result back to its caller.
9. Conclusion
Jump statements are important in Java because they help control the execution flow of a program.
- break: Stops a loop or switch statement.
- continue: Skips the current loop iteration.
- return: Exits a method and can return a value.
Understanding these three statements helps us write programs with better control over execution.
Top comments (0)