Have a question?
Message sent Close
View Categories

What are Jump statements?

What are Jump statements?

📄
filename.js
public class BreakExample {

    public static void main(String[] args) {

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

            if (i == 3) {

                break; // Exit the loop when i is 3

            }

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

        }

    }

}

Output:
i = 1
i = 2

📄
filename.js
public class ContinueExample {

    public static void main(String[] args) {

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

            if (i == 3) {

                continue; // Skip the rest of the loop iteration when i is 3

            }

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

        }

    }

}

Output:
i = 1
i = 2
i = 4
i = 5

📄
filename.js
public class ReturnExample {

    public static void main(String[] args) {

        System.out.println("The sum is " + sum(10, 20));

    }

    public static int sum(int a, int b) {

        return a + b; // Return the sum of a and b

    }

}

Output:
The sum is 30