Unlock Your Python Backend Career: Build 30 Projects in 30 Days. Join now for just $54

Java Control Flow Statements

Jump Statements in Java

Jump statements are used to control the flow of loops by transferring control to different parts of the program.

Break Statement

The break statement is used to terminate a loop immediately when a certain condition is met.

public class BreakExample {
    public static void main(String[] args) {
        for (int i = 1; i <= 10; i++) {
            if (i == 5) {
                break;
            }
            System.out.println(i);
        }
    }
}

Explanation:

  1. The loop runs from 1 to 10.

  2. When i equals 5, the break statement stops the loop.

  3. Output: 1 2 3 4

Continue Statement

The continue statement skips the current iteration and moves to the next iteration.

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

Explanation:

  1. The loop runs from 1 to 10.

  2. When i equals 5, the continue statement skips that iteration.

  3. Output: 1 2 3 4 6 7 8 9 10

Return Statement

The return statement is used to exit from a method and return a value.

public class ReturnExample {
    public static void main(String[] args) {
        System.out.println(checkNumber(5));
    }

    public static String checkNumber(int num) {
        if (num > 0) {
            return "Positive";
        } else if (num < 0) {
            return "Negative";
        } else {
            return "Zero";
        }
    }
}

Explanation:

  1. The method checkNumber checks if a number is positive, negative, or zero.

  2. It returns a string based on the condition.

Conclusion

Control flow statements play a crucial role in programming as they determine the execution flow of a program.

The if-else statements help in decision-making based on conditions, while loops (for, while, do-while) enable repeated execution of code blocks.

Jump statements like break and continue allow better control over loops. Understanding and effectively utilizing these statements leads to efficient and optimized code execution.

By mastering these concepts, programmers can write dynamic and responsive applications that adapt to different scenarios.

Whenever you're ready

There are 4 ways we can help you become a great backend engineer:

The MB Platform

Join 1000+ backend engineers learning backend engineering. Build real-world backend projects, learn from expert-vetted courses and roadmaps, track your learnings and set schedules, and solve backend engineering tasks, exercises, and challenges.

The MB Academy

The “MB Academy” is a 6-month intensive Advanced Backend Engineering BootCamp to produce great backend engineers.

Join Backend Weekly

If you like post like this, you will absolutely enjoy our exclusive weekly newsletter, Sharing exclusive backend engineering resources to help you become a great Backend Engineer.

Get Backend Jobs

Find over 2,000+ Tailored International Remote Backend Jobs or Reach 50,000+ backend engineers on the #1 Backend Engineering Job Board