🎓 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
1. Introduction
In Java, break and continue are two important control flow statements used within loops. break is used to exit the loop entirely, skipping any remaining iterations. continue, on the other hand, is used to skip the current iteration and proceed with the next iteration of the loop.
2. Key Points
1. break terminates the loop immediately, and control resumes at the first statement following the loop.
2. continue skips the current iteration and move to the end of the loop body, proceeding with the next iteration.
3. break can be used in all types of loops as well as in switch statements.
4. continue is only used in loops and cannot be used in switch statements.
3. Differences
| break | continue |
|---|---|
| Used to exit from the loop immediately, regardless of its condition. | Skips the rest of the code inside the loop for the current iteration and jumps to the next iteration of the loop. |
| It can be used in loops (for, while, and do-while) and switch statements. | It is only used in loops (for, while, and do-while); it cannot be used in switch statements. |
| After executing a break statement, the control moves to the statement immediately following the loop or switch. | After a continue statement is executed, the control moves to the beginning of the loop for the next iteration, skipping the remaining code in the loop body for the current iteration. |
| Used to terminate the loop or to exit from a switch case. | Used to skip the current iteration based on a specific condition without terminating the loop. |
| It can be used to escape early from a loop when a certain condition is met, avoiding unnecessary or irrelevant further iterations. | This is used when you want to avoid the current iteration due to a specific condition but still continue looping through the remaining iterations. |
4. Example
// Example of break
System.out.println("Example of break:");
for (int i = 1; i <= 5; i++) {
// If the value of i is 3, break out of the loop
if (i == 3) {
break;
}
System.out.println(i);
}
// Example of continue
System.out.println("\nExample of continue:");
for (int i = 1; i <= 5; i++) {
// If the value of i is 3, skip this iteration
if (i == 3) {
continue;
}
System.out.println(i);
}
Output:
Example of break: 1 2 Example of continue: 1 2 4 5
Explanation:
1. In the break example, the loop prints numbers 1 and 2, and as soon as it reaches 3, the break statement terminates the loop.
2. In the continue example, the loop prints numbers 1 and 2, skips number 3 due to the continue statement, and then resumes by printing numbers 4 and 5.
5. When to use?
- Use break when you need to exit a loop immediately based on a certain condition or to terminate a switch statement.
- Use continue when you want to skip the rest of the current loop iteration and continue with the next iteration.
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