🎓 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 continue statement in Java is a control flow statement that is used to skip the current iteration of a loop and proceed to the next iteration. It is commonly used within for, while, and do-while loops to control the flow of the loop based on certain conditions.
Table of Contents
- What is a continue Statement?
- Syntax of continue Statement
- How continue Statement Works
- Simple continue Statement Example
- continue Statement in a for Loop
- continue Statement in a while Loop
- continue Statement in a do-while Loop
- continue Statement in Nested Loops
- Using continue with Labels
- Conclusion
What is a continue Statement?
The continue statement is used to skip the remainder of the code in the current iteration of a loop and immediately proceed to the next iteration. This is particularly useful when you want to skip certain iterations based on specific conditions.
Syntax of continue Statement
Syntax:
continue;
How continue Statement Works
- When the
continuestatement is encountered, the remaining code in the loop's current iteration is skipped. - The loop proceeds to the next iteration.
- In a
forloop, the update expression is executed before the next iteration. - In
whileanddo-whileloops, the condition is re-evaluated before the next iteration.
Simple continue Statement Example
Example:
public class SimpleContinueExample {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue;
}
System.out.println("i: " + i);
}
}
}
Explanation: This loop prints the numbers 1, 2, 4, and 5. When i equals 3, the continue statement skips the remainder of the loop body, so 3 is not printed.
continue Statement in a for Loop
The continue statement can be used within a for loop to skip specific iterations based on a condition.
Example:
public class ContinueInForLoop {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
continue;
}
System.out.println("Odd number: " + i);
}
}
}
Explanation: This loop prints only the odd numbers between 1 and 10 by skipping the even numbers.
continue Statement in a while Loop
The continue statement can also be used within a while loop to skip specific iterations.
Example:
public class ContinueInWhileLoop {
public static void main(String[] args) {
int i = 1;
while (i <= 10) {
if (i % 2 == 0) {
i++;
continue;
}
System.out.println("Odd number: " + i);
i++;
}
}
}
Explanation: This loop prints only the odd numbers between 1 and 10 by skipping the even numbers.
continue Statement in a do-while Loop
The continue statement can be used within a do-while loop to skip specific iterations.
Example:
public class ContinueInDoWhileLoop {
public static void main(String[] args) {
int i = 1;
do {
if (i % 2 == 0) {
i++;
continue;
}
System.out.println("Odd number: " + i);
i++;
} while (i <= 10);
}
}
Explanation: This loop prints only the odd numbers between 1 and 10 by skipping the even numbers.
continue Statement in Nested Loops
The continue statement can be used within nested loops to skip iterations of the inner loop.
Example:
public class ContinueInNestedLoops {
public static void main(String[] args) {
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
if (j == 2) {
continue;
}
System.out.println("i: " + i + ", j: " + j);
}
}
}
}
Explanation: This loop skips the inner loop's iteration when j equals 2, so it does not print pairs where j is 2.
Using continue with Labels
Labels can be used with the continue statement to specify which loop to continue.
Example:
public class ContinueWithLabel {
public static void main(String[] args) {
outerLoop: for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
if (j == 2) {
continue outerLoop;
}
System.out.println("i: " + i + ", j: " + j);
}
}
}
}
Explanation: This loop uses a label outerLoop and continues the outer loop when j equals 2, skipping to the next iteration of the outer loop.
Conclusion
The continue statement in Java is a useful control flow tool that allows you to skip the current iteration of a loop and proceed to the next iteration. Understanding how to use continue 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