Java Fundamentals

Jumping Statements in Java

by Passion2Code

Jumping statements in Java, like break, continue and return, give you the power to control the flow of your program. These handy tools let you exit loops, skip iterations, or end a method when needed.

Think of it like real life: ever wanted to skip a boring task or leave an event early? Jumping statements in Java work the same way, helping you navigate your code efficiently and make quick adjustments when the situation calls for it.

Jumping statements in Java, like break, continue and return, give you the power to control the flow of your program. These handy tools let you exit loops, skip iterations, or end a method when needed.

Jumping Statements


Flow Control Statements

These are the statements that control the flow of the execution of a program. Flow control statements are essential programming tools that help manage the execution flow of your Java program. They dictate the sequence in which various statements, instructions, or function calls are carried out. With these statements, you can:

  • Make decisions based on specific conditions
  • Repeat code blocks multiple times
  • Navigate to different sections of the program
  • Effectively manage complex program logic.

There are three ways to control flow, by using.

  1. Conditional/Decision-Making statements
  2. Looping statements
  3. Jumping statement

Jumping Statements

Jumping statements are a type of control flow mechanism that allows you to transfer control from one part of the program to another. You can use them to break out of a loop, skip the current iteration, or exit a method entirely.

Java provides three main jumping statements:

  1. break Statement
  2. continue Statement
  3. return Statement

1. Break Statement

  • The break statement is like an emergency exit, it lets you break out of a loop or a switch statement when a certain condition is met.
  • It stops the execution of the loop or switch and moves the program control to the next statement after the loop or switch.

The break statement is like an emergency exit, it lets you break out of a loop or a switch statement when a certain condition is met.
It stops the execution of the loop or switch and moves the program control to the next statement after the loop or switch.

Syntax:
break;
Java
Example:

Let’s say we want to find the first number divisible by 5 in a list of numbers, and once we find it, we stop searching:

Java
public class BreakExample {
    public static void main(String[] args) {
        int[] numbers = {1, 3, 4, 6, 10, 15};
        
        for (int num : numbers) {
            if (num % 5 == 0) {
                System.out.println("First number divisible by 5 is: " + num);
                break;  // Breaks the loop once the condition is met
            }
        }
    }
}

Explanation:

Data Types in Java
Data Types in Java
  1. We loop through the array of numbers.
  2. When we find the first number divisible by 5 (in this case, 10), the break statement exits the loop immediately.

Output:

First number divisible by 5 is: 10

2. Continue Statement

  • The continue statement is used to skip the current iteration of a loop and move on to the next iteration.
  • It doesn’t stop the loop entirely but just tells it to skip to the next cycle.

The continue statement is used to skip the current iteration of a loop and move on to the next iteration.
It doesn’t stop the loop entirely but just tells it to skip to the next cycle.

Syntax:
continue;
Java
Example:

Let’s print all the numbers from 1 to 10 except for 5:

Java
public class ContinueExample {
    public static void main(String[] args) {
        for (int i = 1; i <= 10; i++) {
            if (i == 5) {
                continue;  // Skip the iteration when i is 5
            }
            System.out.println(i);
        }
    }
}

Explanation:

  1. The loop goes through numbers 1 to 10.
  2. When i equals 5, the continue statement is executed, skipping the rest of the loop body for that iteration and moving to the next iteration.

Output:

1
2
3
4
6
7
8
9
10

3. Return Statement

  • The return statement is used to exit a method and optionally return a value.
  • It stops the execution of the current method and transfers control to the calling method.

The return statement is used to exit a method and optionally return a value.
It stops the execution of the current method and transfers control to the calling method.

Syntax:
return;

//or, if you want to return a value:

return value;
Java
Example:

Let’s write a method that returns the square of a number, and exits as soon as we return the result:

Java
public class ReturnExample {
    public static void main(String[] args) {
        System.out.println("Square of 5 is: " + square(5));
    }

    public static int square(int num) {
        return num * num;  // Exits the method and returns the square of the number
    }
}

Explanation:

  1. We call the square() method with the number 5.
  2. The return statement exits the method and returns the result of num * num.

Output:

Square of 5 is: 25

When to Use Jumping Statements?

  • Use a break when you want to exit a loop or switch once a specific condition is met (like stopping a search after finding the result).
  • Use continue when you want to skip an iteration of a loop but keep the loop going (like skipping certain numbers in a sequence).
  • Use return when you want to exit a method early, possibly returning a value (like returning the result of a calculation).

Summary

Jumping statements are powerful tools that help control the flow of your program.

Variables in Java are like containers that store data values, making them essential for performing calculations, holding temporary results, and enabling dynamic application behavior.
Variables in Java
  • break: Exits a loop or switch statement.
  • continue: Skips the current iteration of a loop and moves to the next one.
  • return: Exits a method and optionally returns a value.

Must Read

Decision-Making Statements in Java
– Learn how to control the flow of your program using if, if-else, switch, and other decision-making constructs.

Looping Statements in Java
– Master the art of iteration with for, while, and do-while loops in Java.

Keywords in Java
– Learn about the reserved keywords in Java and their specific functions.

Java User Input
– Learn how to handle user input in Java using Scanner class and other methods.

Operators in Java
– Get to know the various operators in Java and how to use them in expressions.


FAQ’S

What is the main purpose of jumping statements in Java?
Jumping statements in Java are used to alter the normal flow of control in a program. They allow you to jump to a specific part of the code, skip iterations, or exit loops or functions prematurely.

What happens if I use a break in a nested loop?
The break statement will exit only the innermost loop. If you want to exit multiple nested loops, you can use labeled breaks (which will cover in advanced Java concepts).

Can I use continue with while and for loops?
Yes, you can use continue in both while and for loops to skip an iteration based on a condition.

Can a method have multiple return statements?
Yes! A method can have multiple return statements, but only one will be executed. The method will exit as soon as a return is encountered.

Leave a Comment

Index