What is Break and Continue Statement in Java?

Understand how break and continue statements work in Java to control flow within loops.

Get Started free
Home Guide What is Break and Continue Statement in Java?

What is Break and Continue Statement in Java?

Control flow statements like break and continue are crucial for managing loop behavior in Java.

These statements modify the normal flow, allowing more control over the execution process within constructs like for, while, and do-while loops.

This article explains the usage and importance of break and continue statements in Java, which simplify loop logic and improve code efficiency.

What is a Break Statement in Java?

The break statement immediately exits a loop, regardless of whether the loop condition remains true. It is generally applied when a specific condition has been met and further iteration is unnecessary.

Syntax:

break;

Example:

for (int i = 0; i < 10; i++) {

if (i == 5) {

break; // loop breaks when i = 5

}

System.out.println(i);

}

Also Read: Assert in Java

What is a Continue Statement in Java?

The continue statement skips the current iteration of a loop and proceeds with the next cycle. Unlike break, continue statement does not terminate the loop but bypasses the remaining statements in the current iteration.

Syntax:

continue;




Example:

for (int i = 0; i < 10; i++) {

if (i == 5) {

continue; // loop runs until i<10 and skips i=5 iteration

}

System.out.println(i);

}

Importance of Break and Continue Statements in Java

The break and continue statements in Java are helpful tools for managing loop behavior. Here’s why they are essential:

  1. Efficiency: The break statement allows exiting a loop early when it’s no longer needed, saving time and resources. Whereas, continue statement skips current iteration and moves to the next one, avoiding unnecessary code execution.
  2. Simpler Code: These statements make loops easier to understand by handling specific conditions directly without complicated logic.
  3. Better Control: The break statement stops the loop once a condition is met. The continue statement skips certain iterations without stopping the entire loop.
  4. Improved Readability: Break and continue statements make the code cleaner and easier to follow by reducing the need for extra checks inside the loop.

How to Use Break and Continue Statements in a While Loop

Following example shows how Break and Continue is used inside While Loop:

public class Example {

public static void main(String[] args) {

System.out.println("Break Example:");

for (int i = 0; i < 10; i++) {

if (i == 4) break;

System.out.print(i + " ");

}

System.out.println("\nContinue Example:");

for (int j = 0; j < 10; j++) {

if (j == 4) continue;

System.out.print(j + " ");

}

}

}

Output of the above code:

Break Example: 0 1 2 3

Continue Example: 0 1 2 3 5 6 7 8 9

BrowserStack Automate Banner

Why Test on Real Device Cloud?

Testing control flow logic, such as break and continue, on real devices ensures the reliability of applications across different platforms. Testing on BrowserStack’s real device cloud offers several advantages:

  • Real-World Testing: Testing on real devices ensures that the app or website behaves as expected in real user conditions, capturing real-world performance, bugs, and compatibility issues.
  • Access to a Variety of Devices: Provides access to a wide range of physical devices, including different screen sizes, operating systems, and versions.
  • Cost-effectiveness: Eliminates the need for expensive physical device labs. Testers can access the devices they need on demand without the overhead costs of maintaining hardware.

Talk to an Expert

Conclusion

The break and continue statements in Java help make loops more efficient and easier to control. Break stops a loop early, while continue skips to the next iteration.

Using these statements makes code simpler, more transparent, and more efficient, helping developers write cleaner and faster programs.

Tags
Automation Testing Manual Testing Real Device Cloud

Get answers on our Discord Community

Join our Discord community to connect with others! Get your questions answered and stay informed.

Join Discord Community
Discord