This tutorial explains how to use the Java Continue Statement in various scenarios while dealing with the loops with the help of programming examples:
In this tutorial, we will discuss Java’s “Continue Statement” – one of the jump statements that Java supports.
Here, we will cover the topic in detail along with descriptions, programs, examples, etc. Moreover, some frequently-asked questions will also be provided so that you will be aware of the trending questions that are being asked from the topic.
=> Check ALL Java Tutorials Here
Table of Contents:
Jump Statements
Jump statements are those statements that transfer the control of the program to another part of the program. These statements are essential when the user wants the control to come out of the loop or terminate a statement sequence. Java supports these kinds of statements.
It has three jump statements as shown below:
- continue
- break
- return
We will be explaining the continue statement in the latter part of this tutorial. The other two jump statements will be covered in the upcoming tutorials.
Java Continue Statement
Sometimes, you might want to keep executing the loop but don’t want to process the code for a particular iteration. This type of scenario is achievable with the Continue Java statement.
Skipping The Value In A Loop
Here, we are taking a small example to demonstrate how we can use continue in Java, especially in a loop.
We have initialized a “for loop” and a continue statement is specified after condition (i=2). This will make the loop skip the value of ‘i’ when (i=2) upon printing the value of ‘i’.
public class example { public static void main(String[] args) { /* * Initialized a for loop and * a continue statement is specified after * condition (i==2). * This will make the loop skip the value of 2. */ for (int i = 0; i < 5; i++) { if (i == 2) { continue; } System.out.println(i); } } }
Output
Printing The Numbers
Now, we will use the continue statement to print two numbers on the same line delimited by space.
In the below example, we have initialized a for-loop and then specified a condition to check whether ‘i’ is even or not. In case, the value of ‘i’ is even then the loop will continue without printing a new line.
public class example { public static void main(String[] args) { // Initializied for-loop for (int i = 0; i < 10; i++) { // Printing the value of 'i' System.out.print(i + " "); /* * If 'i' is even, then the loop will * continue without printing a new line. */ if (i % 2 == 0) continue; System.out.println(""); } } }
Output
Continue Java With A for-Each Loop
In this example, we will demonstrate how to use the Continue statement in a for-each loop. Here, we have initialized an array that contains five numbers.
Then, we have started a for-each loop. If the element of an array matches the value = 15, then it will make the loop skip the value of 15.
public class example { public static void main(String[] args) { // initialized an array of five elements int[] arr = {01, 05, 10, 15, 20}; /* * for each loop stats here. If the element of an array matches the * value = 15, then it will make the loop skip the value of 15 */ for(int num : arr){ if(num == 15){ continue; } System.out.println(num); } } }
Output
Continue Java With A While Loop
In this section, we will demonstrate how to use a continue statement inside a Java while loop. Initially, we have taken an input variable with some value. Then, we have initialized a while loop where we have put a condition (i==2) followed by the continue statement.
When (i==2), the value of ‘i’ will be skipped in the output and the execution will continue as usual.
public class example { public static void main(String[] args) { int i = 5; while (i >= 0) { /* * when i==2, the value of 'i' will be skipped * in the output and the execution will continue. */ if (i == 2) { i = i-1; continue; } System.out.println(i); i = i-1; } } }
Output
Continue Java With A Do-While Loop
In this section, we will demonstrate how to use a continue statement inside a do-while loop. First of all, we have initialized a variable ‘i’ with 0. Then, we started a do-while loop.
In the do-while loop, we have put a condition that when i==2, the value of ‘i’ will be skipped in the output and the execution will continue till ‘i’ is less than or equal to 5.
public class example { public static void main(String[] args) { int i = 0; do { /* * when i==2, the value of 'i' will be skipped in the output and the * execution will continue till i is less than or equal to 5. */ if (i == 2) { i++; continue; } System.out.print(i + " "); i++; } while (i <= 5); } }
Output
Java Break Vs Continue
In the Java break vs continue topic, we will list down the major differences in the Java continue and break statements.
Break Java Statement | Continue Java Statement |
---|---|
? It is used to jump out of the loop. | ? It is used to break a single iteration. |
? The loop gets terminated or halted permanently when the break statement condition matches. | ? The loop gets halted only for a single iteration when the continue Java statement condition matches. |
? It is very commonly used with the “switch statements”. | ? It is not compatible with the “switch statements”. |
Frequently Asked Questions
Q #1) What is Java continue?
Answer: Continue statement in Java is a jump statement that is used to transfer the control to another part of the program.
Q #2) Can we use Continue in Java?
Answer: Yes, we can use continue in Java by specifying some conditions inside a loop.
Q #3) Do While loop continue?
Answer: Yes, you can use a continue statement Java with a while loop (as illustrated above).
Conclusion
In this tutorial, we have explained one of the important Jump Statement i.e. Java Continue statement. This is a small topic but is very useful while dealing with the loops. We have properly demonstrated the various scenarios/condition in which you can use the continue statement.
Proper examples, FAQs, and programs that are sufficient to make the topic understandable are provided.
In the upcoming tutorial, we will cover the other Jump statements that we have listed at the beginning of this tutorial.
=> Visit Here For The Exclusive Java Training Tutorial Series