Back to Java
2025-12-226 min read

continue (Java)

Learn continue (Java) step by step with clear examples and exercises.

Why This Matters

In this comprehensive tutorial, we will delve into the intricacies of the continue statement in Java. The continue statement plays a crucial role in optimizing loops and handling specific scenarios within your code. By understanding its usage, you can write cleaner, more efficient programs that perform well under various conditions.

Why This Matters

The continue statement helps control the flow of a loop by skipping certain iterations based on conditions. This can be particularly useful for filtering out unwanted data, optimizing performance, and handling edge cases in your code. In addition, mastering the continue statement will enable you to write more flexible and robust programs that can handle complex scenarios with ease.

Prerequisites

Before diving into the continue statement, it's important to have a solid understanding of Java's loop structures, such as for, while, and do-while. Familiarity with basic data types, variables, and control statements will also be beneficial. To fully grasp the concepts presented in this tutorial, we recommend that you have experience working with arrays, methods, and exception handling.

Core Concept

The continue statement is used within loops to skip the current iteration and move on to the next one. When a continue statement is encountered inside a loop, the current iteration is skipped, and the loop proceeds to the next iteration. Here's a simple example using a for loop:

int[] numbers = {1, 2, 3, 4, 5, 6};

for (int i = 0; i < numbers.length; i++) {
if (numbers[i] % 2 == 0) { // If the number is even
continue; // Skip this iteration and move to the next one
}
System.out.println(numbers[i]);
}

In this example, we have an array of numbers. The loop iterates through each element in the array. When it encounters an even number, the continue statement is executed, and the loop skips to the next iteration without printing the current number. The remaining odd numbers are printed.

Using continue with multiple conditions

In some cases, you might want to use multiple conditions to determine whether to skip an iteration. Here's an example where we skip both even numbers and multiples of 3:

int[] numbers = {1, 2, 3, 4, 5, 6};

for (int i = 0; i < numbers.length; i++) {
if ((numbers[i] % 2 == 0) || (numbers[i] % 3 == 0)) { // If the number is even or a multiple of 3
continue; // Skip this iteration and move to the next one
}
System.out.println(numbers[i]);
}

In this example, both even numbers and multiples of 3 are skipped during the loop iterations. The remaining numbers that meet neither condition are printed.

Worked Example

Let's consider a more complex example where we need to find all prime numbers between 1 and 100 using the continue statement:

public class PrimeNumbers {
public static void main(String[] args) {
for (int i = 2; i <= 100; i++) {
boolean isPrime = true;

// Check divisibility by 2 and subsequent odd numbers up to the square root of i
for (int j = 2; j <= Math.sqrt(i); j += 2) {
if (i % j == 0) {
isPrime = false;
break;
}
}

// If the number is prime, print it
if (isPrime) {
System.out.println(i);
}
}
}
}

In this example, we use nested loops to check for divisibility by all numbers up to the square root of i. If a number is divisible by any of these, it's not prime, and we set isPrime to false. Once we find a prime number, we print it.

Common Mistakes

  1. ### Forgetting the semicolon after the continue statement:
for (int i = 0; i < numbers.length; ) { // Missing semicolon at the end of the loop declaration
if (numbers[i] % 2 == 0) {
continue // This line should have a semicolon after it
}
System.out.println(numbers[i]);
i++;
}
  1. ### Using continue in a place where it doesn't make sense:
for (int i = 0; i < numbers.length; i++) {
if (numbers[i] > 10) { // If the number is greater than 10, we can't skip iterations because we need to process all numbers
continue; // This will cause an error
}
System.out.println(numbers[i]);
}
  1. ### Using continue in a loop that doesn't have a condition:
int i = 0;
while (true) { // An infinite loop
if (i > 10) {
continue; // This will cause an infinite loop because we never exit the while loop
}
System.out.println(i);
i++;
}
  1. ### Using continue with a non-loop construct:
if (numbers[0] > 10) { // This is not a loop, so continue has no effect
continue; // This will cause a compilation error
}
System.out.println(numbers[0]);

Practice Questions

  1. Write a program that finds all the even numbers in an array using the continue statement.
  2. Modify the prime number example to print only the first 20 prime numbers between 1 and 100.
  3. Given an array of strings, write a program that removes all empty strings from the array using the continue statement.
  4. Write a program that finds the sum of all odd numbers in an array using the continue statement.
  5. Modify the prime number example to find all prime numbers between 1 and 200,000.
  6. Given a list of names, write a program that removes duplicates using the continue statement.
  7. Write a program that finds the product of all even numbers in an array using the continue statement.
  8. Modify the prime number example to find all prime numbers between 2 and your own input limit (use Scanner).

FAQ

### Can I use the continue statement in a do-while loop?

Yes, you can use the continue statement in both for, while, and do-while loops.

### What happens when I use continue inside an empty loop?

If you use continue inside an empty loop (i.e., a loop with no iterations), nothing will happen because the loop doesn't execute any iterations to skip.

### Can I use the continue statement in a switch case?

No, the continue statement cannot be used within a switch case. Instead, you can use break to exit the entire switch block or jump to another case.

### Is it possible to nest multiple continue statements within a loop?

Yes, it is possible to nest multiple continue statements within a loop. However, this can lead to complex and hard-to-read code. It's recommended to use them sparingly and only when necessary for optimizing performance or handling specific scenarios.

### What happens when I use continue with a label?

A labeled continue statement skips the iteration of an outer loop that shares the same label as the continue statement. This can be useful for complex multi-level loops where you need to control the flow across multiple nested loops.

### Can I use the continue statement with a break inside a loop?

Yes, it is possible to combine both continue and break statements within a single loop. The break statement exits the entire loop, while the continue statement skips the current iteration and continues with the next one. This can be useful for handling complex scenarios where you need to exit a loop early but still process some iterations before doing so.

### What is the difference between using break and continue in a loop?

The break statement exits the entire loop, while the continue statement skips the current iteration and continues with the next one. The break statement can be used to exit a loop early when a specific condition is met, whereas the continue statement is used to skip iterations based on certain conditions.

### Can I use the continue statement in a foreach loop?

No, the continue statement cannot be used directly within a foreach loop. Instead, you can convert the foreach loop into a for-each enhanced loop (for-each loop with explicit iteration variable) and use the continue statement there. Here's an example:

int[] numbers = {1, 2, 3, 4, 5, 6};

for (int number : numbers) { // This is a foreach loop
if (number % 2 == 0) { // If the number is even
continue; // Skip this iteration and move to the next one
}
System.out.println(number);
}

In this example, we convert the foreach loop into a for-each enhanced loop by declaring an explicit iteration variable (number) and using it in the loop body. Now, we can use the continue statement as before.

continue (Java) | Java | XQA Learn