- Java Tutorial
- Java Introduction
- Java Features
- Java Simple Program
- JVM, JDK and JRE
- Java Syntax
- Java Comments
- Java Keywords
- Java Variables
- Java Literals
- Java Separators
- Java Datatypes
- Java Operators
- Java Statements
- Java Strings
- Java Arrays
- Control Statement
- Java If
- Java If-else
- Java If-else-if
- Java Nested If
- Java Switch
- Iteration Statement
- Java For Loop
- Java For Each Loop
- Java While Loop
- Java Do While Loop
- Java Nested Loop
- Java Break/Continue
- Java Methods
- Java Methods
- Java Method Parameters
- Java Method Overloading
- Java Recursion
- Java OOPS
- Java OOPs
- Java Classes/Objects
- Java Inheritance
- Java Polymorphism
- Java Encapsulation
- Java Abstraction
- Java Modifiers
- Java Constructors
- Java Interface
- Java static keyword
- Java this keyword
- Java File Handling
- Java File
- Java Create File
- Java Read/Write File
- Java Delete File
- Java Program To
- Add Two Numbers
- Even or Odd Numbers
- Reverse a String
- Swap Two Numbers
- Prime Number
- Fibonacci Sequence
- Palindrome Strings
- Java Reference
- Java String Methods
- Java Math Methods
Java Break and Continue Statements
While loops and for-loops are excellent for repeating tasks, you often need more granular control over how they behave. Sometimes you need to stop a loop early because you found the data you were looking for, or perhaps you want to skip over specific items that don't meet your criteria. In Java, the break and continue statements are your primary tools for managing this flow.
Break Statement:
- The
breakstatement acts as an "emergency exit" for your loop. - When Java encounters a
break, it immediately terminates the loop it is currently in, regardless of the loop's conditional expression. - Program execution jumps directly to the first line of code following the loop's closing brace.
Real-World Example: Searching for an Item
Imagine you are searching through an array of IDs. Once you find the ID you need, there is no reason to keep looking through the rest of the list. Using break saves processing power and time.
int[] userIds = {101, 202, 305, 404, 500};
int searchTarget = 305;
for (int i = 0; i < userIds.length; i++) {
if (userIds[i] == searchTarget) {
System.out.println("User found at index: " + i);
break; // Stop looking, we found it!
}
System.out.println("Checking index " + i + "...");
}
// Control jumps here after the break
Output:
Checking index 0...
Checking index 1...
User found at index: 2
break will exit all nested loops. In reality, break only exits the innermost loop it is placed in. If you have a loop inside a loop, the outer loop will continue running.
break to improve efficiency. If your loop is designed to find a single result, breaking immediately after finding that result prevents unnecessary iterations, which is crucial when working with large datasets.
Continue Statement:
- The
continuestatement is used to "skip" the remainder of the current loop iteration. - Unlike
break, it doesn't stop the loop entirely. Instead, it jumps back to the top of the loop to begin the next iteration. - In a
forloop, the update expression (e.g.,i++) is still executed before the next cycle begins.
Real-World Example: Filtering Data
Suppose you are processing a list of transactions, but you only want to print the details of transactions that are greater than $100. You can use continue to skip the small ones.
double[] transactions = {120.50, 45.00, 300.25, 12.99, 150.00};
for (double amount : transactions) {
if (amount < 100.00) {
continue; // Skip this transaction and move to the next one
}
System.out.println("Processing large transaction: $" + amount);
}
Output:
Processing large transaction: $120.5
Processing large transaction: $300.25
Processing large transaction: $150.0
continue inside while loops. If your increment logic (like i++) is at the bottom of the loop and you trigger a continue above it, you might skip the increment entirely, leading to an infinite loop.
continue to avoid "Arrow Code" (deeply nested if-statements). By skipping invalid data early using continue, you can keep your main logic closer to the left margin, making it much easier to read.
Use Cases:
- break: Use this when a specific condition makes the rest of the loop's iterations unnecessary or impossible (e.g., finding a file, encountering an error, or reaching a user-defined limit).
- continue: Use this when you want to filter out specific "bad" or "irrelevant" data points while still ensuring the loop processes every other item in the collection.
Summary
The break and continue statements are powerful flow-control tools that make your Java loops smarter and more efficient. Use break to exit a loop entirely when your job is done, and use continue to bypass specific iterations that don't meet your criteria. Mastering these will help you write cleaner code and handle complex logic within your iterations more effectively.