🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
The break statement in Java is a control flow statement that is used to terminate the loop or switch statement in which it is present. It immediately transfers control to the statement following the loop or switch, effectively ending the current iteration or switch case.
Table of Contents
- What is a break Statement?
- Syntax of break Statement
- How break Statement Works
- Simple break Statement Example
- break Statement in a for Loop
- break Statement in a while Loop
- break Statement in a do-while Loop
- break Statement in a Switch Case
- Using break with Labels
- Conclusion
What is a break Statement?
The break statement is used to exit a loop or switch statement prematurely. It is particularly useful when you want to terminate a loop based on a certain condition or to end a case in a switch statement.
Syntax of break Statement
Syntax:
break;
How break Statement Works
- When the
breakstatement is encountered inside a loop or switch statement, the control immediately exits the loop or switch. - The statement following the loop or switch is executed next.
Simple break Statement Example
Example:
public class SimpleBreakExample {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
if (i == 5) {
break;
}
System.out.println("i: " + i);
}
}
}
Explanation: This loop prints the numbers 1 to 4. When i equals 5, the break statement terminates the loop.
break Statement in a for Loop
The break statement can be used within a for loop to exit the loop based on a specific condition.
Example:
public class BreakInForLoop {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
if (i == 7) {
break;
}
System.out.println("i: " + i);
}
}
}
Explanation: This loop prints numbers from 1 to 6. When i equals 7, the break statement exits the loop.
break Statement in a while Loop
The break statement can be used within a while loop to exit the loop based on a specific condition.
Example:
public class BreakInWhileLoop {
public static void main(String[] args) {
int i = 1;
while (i <= 10) {
if (i == 7) {
break;
}
System.out.println("i: " + i);
i++;
}
}
}
Explanation: This loop prints numbers from 1 to 6. When i equals 7, the break statement exits the loop.
break Statement in a do-while Loop
The break statement can be used within a do-while loop to exit the loop based on a specific condition.
Example:
public class BreakInDoWhileLoop {
public static void main(String[] args) {
int i = 1;
do {
if (i == 7) {
break;
}
System.out.println("i: " + i);
i++;
} while (i <= 10);
}
}
Explanation: This loop prints numbers from 1 to 6. When i equals 7, the break statement exits the loop.
break Statement in a Switch Case
The break statement is used in a switch statement to terminate a case and prevent the execution from falling through to subsequent cases.
Example:
public class BreakInSwitchCase {
public static void main(String[] args) {
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;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
default:
System.out.println("Weekend");
break;
}
}
}
Explanation: This program prints "Wednesday" for day equals 3 and then exits the switch statement using break.
Using break with Labels
Labels can be used with the break statement to specify which loop to break out of in nested loops.
Example:
public class BreakWithLabel {
public static void main(String[] args) {
outerLoop: for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
if (j == 2) {
break outerLoop;
}
System.out.println("i: " + i + ", j: " + j);
}
}
}
}
Explanation: This loop uses a label outerLoop and exits the outer loop when j equals 2, skipping the remaining iterations of both the inner and outer loops.
Conclusion
The break statement in Java is a powerful control flow tool that allows you to terminate loops and switch statements prematurely based on specific conditions. Understanding how to use break effectively, including within nested loops and with labels, can help you write more efficient and readable code.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment