OBJECT ORIENTED PROGRAMMING THROUGH JAVA : Unit - 1 : Break and Continue Statements , Ternary Statement

break, continue, and return in Java – Detailed Notes with Examples

These three keywords in Java are used to control the flow of execution in loops and methods. Each has a distinct role in loop and method control.


1. break Keyword

The break statement is used to terminate a loop or switch-case block immediately. Control jumps to the statement immediately after the loop or switch block.

Syntax:

break;

Example – Break in a for Loop:

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

Output:

1
2
3
4

Use Cases:

  • To exit early from a loop when a condition is met
  • In switch cases to prevent fall-through

๐Ÿ”ธ 2. continue Keyword

The continue statement skips the current iteration of the loop and continues with the next iteration.

Syntax:

continue;

Example – Continue in a for Loop:

public class ContinueDemo {
    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

Use Cases:

  • To skip a specific condition inside a loop
  • Useful when you want to avoid processing certain values

3. return Keyword

The return statement is used to exit from a method and optionally return a value to the caller. It is commonly used in functions to pass back results.

Syntax:

// For void methods
return;

// For value-returning methods
return value;

๐Ÿงช Example – Return with Value:

public class ReturnDemo {
    public static int square(int n) {
        return n * n;
    }

    public static void main(String[] args) {
        int result = square(4);
        System.out.println("Square of 4 is " + result);
    }
}

Output:

Square of 4 is 16

Example – Return without Value:

public class ReturnExample {
    public static void printEven(int n) {
        if (n % 2 != 0) {
            System.out.println("Not an even number");
            return; // exits method early
        }
        System.out.println("Even number: " + n);
    }

    public static void main(String[] args) {
        printEven(7);
        printEven(8);
    }
}

Output:

Not an even number
Even number: 8

Use Cases:

  • To return a value from a method
  • To exit a method early based on a condition

Summary Table

Keyword Purpose Used In Effect
break Exit loop or switch early Loops, switch Jumps out of block
continue Skip current iteration Loops Goes to next iteration
return Exit method and return value Methods Ends method execution

Final Notes:

  • break and continue only affect loops and switch blocks
  • return is used in methods to control execution and return results
  • Use them wisely to make your control flow clear and maintainable

Comments

Popular posts from this blog

How to Get a Job in Top IT MNCs (TCS, Infosys, Wipro, Google, etc.) – Step-by-Step Guide for B.Tech Final Year Students

Common HR Interview Questions

How to Get an Internship in a MNC