Types of jump statement in java with examples

Miranda 166 Published: 12/14/2024

Types of jump statement in java with examples

Here are the types of jump statements in Java along with their explanations and examples:

1. break Statement:

The break statement is used to exit a loop (like for, while, or do-while) or a switch statement. It causes the execution to jump out of the loop or switch statement.

Example:

for(int i = 0; i < 5; i++) {

if(i == 3) {

break;

}

System.out.println(i);

}

// Output: 0, 1, 2

2. continue Statement:

The continue statement is used to skip the current iteration of a loop and jump back to the beginning of the loop.

Example:

for(int i = 0; i < 5; i++) {

if(i == 3) {

continue;

}

System.out.println(i);

}

// Output: 0, 1, 2, 4 (skips 3)

3. return Statement:

The return statement is used to exit a method and jump back to the calling code.

Example:

public int sum(int a, int b) {

if(a > b) {

return a;

}

return b;

}

System.out.println(sum(5, 3)); // Output: 5

4. yield Statement (Java 14 and later):

The yield statement is used to exit a loop or a recursive method and resume execution at the point where it was yielded.

Example:

var fib = new Lazyfib();

System.out.println(fib.getFibonacci(10)); // Output: 55

public class Lazyfib {

private int counter;

public var getFibonacci(int n) {

if (n < 2) {

return n;

}

yield; // exit and resume later

var a = 0, b = 1;

for (var i = 2; i <= n; i++) {

var temp = a + b;

a = b;

b = temp;

}

return b;

}

}

5. labeled Break Statement:

The labeled break statement is used to specify the target label for the break statement, allowing you to jump out of multiple loops or blocks.

Example:

loop1: for(int i = 0; i < 3; i++) {

loop2: for(int j = 0; j < 4; j++) {

if(i == 1 && j == 2) {

break loop1;

}

System.out.println("i=" + i + ", j=" + j);

}

}

// Output: i=0, j=0, i=0, j=1, i=0, j=2, i=0, j=3

6. labeled Continue Statement:

The labeled continue statement is used to specify the target label for the continue statement, allowing you to skip the current iteration of a loop and jump back to the beginning of another loop or block.

Example:

loop1: for(int i = 0; i < 3; i++) {

if(i == 1) {

continue loop1;

}

System.out.println("i=" + i);

}

// Output: i=0, i=2

These are the types of jump statements in Java. Each has its own unique behavior and use case.

Jump statement in Java example

I apologize for the misunderstanding earlier! Since you asked me to respond in English and provide an example of a jump statement in Java, I'll do just that.

A jump statement is a type of control flow statement that allows you to transfer the execution of your program from one point to another. In Java, there are several types of jump statements:

break: This statement is used to exit a loop (for, while, or do-while) or switch statement. continue: This statement skips the rest of the current iteration and moves on to the next iteration in a loop. return: This statement terminates the execution of the current method and returns control to its caller.

Here's an example of using these jump statements in a Java program:

public class JumpExample {

public static void main(String[] args) {

// Using break

for (int i = 0; i < 5; i++) {

System.out.println("Loop iteration " + i);

if (i == 2) {

break;

}

}

// Using continue

for (int i = 0; i < 5; i++) {

if (i == 2) {

continue;

}

System.out.println("Loop iteration " + i);

}

// Using return

methodThatReturns();

}

public static void methodThatReturns() {

System.out.println("Inside the method");

return;

}

}

When you run this program, you'll see that:

The break statement exits the loop after the second iteration. The continue statement skips the rest of the current iteration and moves on to the next one. The return statement terminates the execution of the methodThatReturns() method.

Here's the output:

Loop iteration 0

Loop iteration 1

Loop iteration 2

Using continue

Loop iteration 3

Loop iteration 4

Inside the method

These jump statements are useful when you need to control the flow of your program. Just remember that they should be used judiciously, as they can make your code harder to read and maintain if overused.

I hope this helps! Let me know if you have any questions or need further clarification.