Data Types and Operators
(Non-Primitive Data Types)
Control Flow Statements
Conditional Statements
Looping Statements
Branching Statements
Object-Oriented Programming (OOP)
Exception Handling
Collections Framework
Overview of Collections
Java I/O
Multithreading
GUI Programming with Swing
Advanced Topics
JAVA CODE
Java Basics
Working with Objects
Arrays, Conditionals, and Loops
Creating Classes and Applications in Java
More About Methods
Java Applet Basics
Graphics, Fonts, and Color
Simple Animation and Threads
More Animation, Images, and Sound
Managing Simple Events and Interactivity
Creating User Interfaces with the awt
Windows, Networking, and Other Tidbits
Modifiers, Access Control, and Class Design
Packages and Interfaces
Exceptions
Multithreading
Streams and I/O
Using Native Methods and Libraries
Under the Hood
Java Programming Tools
Working with Data Structures in Java
Advanced Animation and Media
Fun with Image Filters
Client/Server Networking in Java
Emerging Technologies
appendix A :- Language Summary
appendix B :- Class Hierarchy Diagrams
appendix C The Java Class Library
appendix D Bytecodes Reference
appendix E java.applet Package Reference
appendix F java.awt Package Reference
appendix G java.awt.image Package Reference
appendix H java.awt.peer Package Reference
appendix I java.io Package Reference
appendix J java.lang Package Reference
appendix K java.net Package Reference
appendix L java.util Package Reference

In Java, ‘break‘, ‘continue‘, and ‘return‘ are control flow statements that alter the flow of execution in loops and methods. Here is a detailed explanation of each:

1. break Statement

The ‘break‘ statement is used to exit from a loop or switch statement before it has completed its normal execution cycle.

Usage in Loops

  • for‘,’ while‘, and ‘do-while‘ loops: When’ break ‘is executed inside these loops, the control immediately exits the loop and proceeds to the next statement following the loop.
				
					for (int i = 0; i < 10; i++) {
    if (i == 5) {
        break; // Exits the loop when i is 5
    }
    System.out.println(i);
}

				
			

Usage in Switch Statements

  • Switch case:’ break‘is commonly used to terminate a case in a ‘switch‘ statement. Without a ‘break‘, the execution will continue to the next case (fall-through).
				
					int day = 3;
switch (day) {
    case 1:
        System.out.println("Monday");
        break;
    case 2:
        System.out.println("Tuesday");
        break;
    case 3:
        System.out.println("Wednesday");
        break; // Exits the switch statement
    default:
        System.out.println("Another day");
}

				
			

2. continue Statement

The ‘continue‘ statement is used to skip the current iteration of a loop and proceed to the next iteration.

Usage in Loops

  • for‘, ‘while‘, and ‘do-while‘ loops: When ‘continue‘ is executed, the control jumps to the next iteration of the loop.
				
					for (int i = 0; i < 10; i++) {
    if (i % 2 == 0) {
        continue; // Skips the current iteration when i is even
    }
    System.out.println(i); // Prints only odd numbers
}

				
			

3. return Statement

The ‘return‘ statement is used to exit from a method, optionally returning a value.

Usage in Methods

  • Void methods: In methods that do not return a value (‘void‘ methods), ‘return‘ is used to exit the method prematurely.
				
					public void printNumbers() {
    for (int i = 0; i < 10; i++) {
        if (i == 5) {
            return; // Exits the method when i is 5
        }
        System.out.println(i);
    }
}

				
			

Non-void methods: In methods that return a value, ‘return‘ must be followed by a value of the corresponding return type.

				
					public int sum(int a, int b) {
    return a + b; // Returns the sum of a and b
}

				
			

Points to Note:

  • Scope:’ break‘ and ‘continue‘ affect only the nearest enclosing loop or switch statement.
  • Return types: When using ‘return‘ in a non-void method, ensure the value matches the method’s declared return type.
  • Code reachability: Statements immediately following a ‘return‘ within the same block are unreachable and will result in a compilation error.

Examples of Each Statement

Example with ‘break‘:

				
					public class BreakExample {
    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            if (i == 3) {
                break; // Loop terminates when i is 3
            }
            System.out.println(i);
        }
        System.out.println("Loop ended.");
    }
}

				
			

Example with ‘continue‘:

				
					public class ContinueExample {
    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            if (i % 2 == 0) {
                continue; // Skips even numbers
            }
            System.out.println(i); // Prints only odd numbers
        }
    }
}

				
			

Example with ‘return‘:

				
					public class ReturnExample {
    public static void main(String[] args) {
        int result = sum(5, 10);
        System.out.println("Sum: " + result);
    }
    
    public static int sum(int a, int b) {
        return a + b; // Returns the sum of a and b
    }
}

				
			

By understanding and correctly using break, continue, and return, you can control the flow of your Java programs more effectively.

Scroll to Top