codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
☕

Java Programming

17 / 65 topics
1Java Intro2Java Get Started3Java Syntax4Java Output5Java Comments6Java Variables7Java Data Types8Java Type Casting9Java Operators10Java Strings11Java Math12Java Booleans13Java If...Else14Java Switch15Java While Loop16Java For Loop17Java Break/Continue18Java Arrays
Tutorials/Java Programming/Java Break/Continue
☕Java Programming

Java Break/Continue

Updated 2026-05-12
15 min read

Java Break/Continue

In this tutorial, we will explore two powerful control statements in Java: break and continue. These statements allow you to alter the normal flow of loops (for, while, and do-while) based on specific conditions. Understanding how to use these statements effectively can help you write more efficient and readable code.

Introduction

The break statement is used to exit a loop prematurely when a certain condition is met. This can save processing time by avoiding unnecessary iterations. On the other hand, the continue statement skips the current iteration of a loop and moves on to the next one, allowing you to skip specific steps within a loop.

Using the Break Statement

Basic Usage

The break statement immediately terminates the loop in which it is placed. Here's a simple example:

BreakExample.java
1public class BreakExample {
2 public static void main(String[] args) {
3 for (int i = 1; i <= 10; i++) {
4 if (i == 5) {
5 break;
6 };
7 System.out.println(i);
8 }
9 }
10}
Output
1
2
3
4

In this example, the loop prints numbers from 1 to 4. When i equals 5, the break statement is executed, and the loop terminates immediately.

Practical Example

Suppose you want to search for a specific number in an array and stop searching once it's found:

SearchExample.java
1public class SearchExample {
2 public static void main(String[] args) {
3 int[] numbers = {10, 20, 30, 40, 50};
4 int target = 30;
5
6 for (int number : numbers) {
7 if (number == target) {
8 System.out.println("Number found: " + number);
9 break;
10 }
11 }
12 }
13}
Output
Number found: 30

Here, the loop iterates through the array and prints the message as soon as it finds the target number. The break statement ensures that the loop doesn't continue unnecessary iterations.

Using the Continue Statement

Basic Usage

The continue statement skips the current iteration of a loop and proceeds to the next one. This can be useful when you want to skip certain conditions within a loop:

ContinueExample.java
1public class ContinueExample {
2 public static void main(String[] args) {
3 for (int i = 1; i <= 10; i++) {
4 if (i % 2 == 0) {
5 continue;
6 }
7 System.out.println(i);
8 }
9 }
10}
Output
1
3
5
7
9

In this example, the loop prints only odd numbers. When i is even (i.e., divisible by 2), the continue statement skips the current iteration and moves to the next one.

Practical Example

Consider a scenario where you want to print all numbers from 1 to 10 except multiples of 3:

SkipMultiples.java
1public class SkipMultiples {
2 public static void main(String[] args) {
3 for (int i = 1; i <= 10; i++) {
4 if (i % 3 == 0) {
5 continue;
6 }
7 System.out.println(i);
8 }
9 }
10}
Output
1
2
4
5
7
8
10

Here, the continue statement skips printing numbers that are multiples of 3.

Common Mistakes

  • Using break outside a loop: The break statement can only be used inside loops (for, while, do-while). Using it elsewhere will result in a compilation error.

  • Misusing continue: Overusing the continue statement can make your code harder to read and maintain. It's generally better to use conditional logic to skip iterations.

Summary

StatementDescription
breakExits the loop immediately when executed.
continueSkips the current iteration of the loop and moves to the next one.

In this tutorial, you learned how to use the break and continue statements in Java loops. These statements provide powerful ways to control the flow of your programs, making them more efficient and easier to manage.

What's Next?

Now that you have a good understanding of loop control statements, we will move on to learn about Java Arrays. Arrays are fundamental data structures in Java that allow you to store multiple values of the same type in a single variable. This knowledge will be essential as you progress further into more complex programming concepts.

Stay tuned for our next tutorial!


PreviousJava For LoopNext Java Arrays

Recommended Gear

Java For LoopJava Arrays