Java continue Statement: Skipping the Current Step in Loops
📘 What Will You Learn in This Guide?
The continue statement in Java loops allows you to skip the current step of the loop when a certain condition is met. This feature is very useful when processing large data sets or simplifying complex conditions. We will examine the use of continue for the for, while and do-while cycles with examples.
🔹 Java continue Statement Basics
The continue command does not execute the code blocks that follow it in the loop it is in. It immediately moves to the next loop step (iteration). This improves the readability of the code in cases where you only want to process certain records.
🔸 1. Using continue in Simple Loops
🧩 A. for Loop (for-each) Example
This command skips the processing of only odd numbers in an integer array and prints only even numbers to the screen.
package com.genixnode.donguler;
public class ContinueForOrnek {
public static void main(String[] args) {
int[] sayiDizisi = { 11, 22, 33, 44, 55, 66, 77 };
// Dizideki sadece çift sayıları işleyelim
for (int sayi : sayiDizisi) {
if (sayi % 2 != 0)
continue; // Tek sayı ise atla
System.out.println("İşleniyor: " + sayi);
}
}
}
💡 This example skips odd numbers and handles even numbers only.
🧮 B. while Loop Example
This command only processes data in directories divisible by 3.
package com.genixnode.donguler;
public class ContinueWhileOrnek {
public static void main(String[] args) {
int[] veriDizisi = { 0, 10, 20, 30, 40, 50, 60, 70, 80 };
int i = 0;
while (i < veriDizisi.length) {
if (i % 3 != 0) {
i++; // Önce dizin artırılmalı!
continue; // Kalan kodu atla
}
System.out.println("İşlenen Dizin: " + i + ", Değer: " + veriDizisi[i]);
i++; // Değer işlendikten sonra artır
}
}
}
⚙️
continue, where it skips the rest of the loop and moves on to the next iteration.
🔸 2. Labeled continue Usage
In nested loops continue skips only the inner loop by default. To skip the current step of the outer loop, it is necessary to use labeled continue.
import java.util.Arrays;
public class EtiketliContinueOrnek {
public static void main(String[] args) {
int[][] matrisDizisi = { { 1, -2, 3 }, { 0, 3, 4 }, { 1, 2, 5 } };
AnaDongu: for (int i = 0; i < matrisDizisi.length; i++) {
boolean hepsiPozitif = true;
for (int j = 0; j < matrisDizisi[i].length; j++) {
if (matrisDizisi[i][j] < 0) {
hepsiPozitif = false;
continue AnaDongu; // Dış döngünün (AnaDongu) bir sonraki adımına geç
}
}
if (hepsiPozitif) {
System.out.println("Tümü pozitif olan dizi işleniyor: " + Arrays.toString(matrisDizisi[i]));
}
}
}
}
🧩 Rows containing negative values are skipped entirely; only positive strings are processed.
📊 Continue Usage Notes
| Feature | Description |
|---|---|
| Area of Use | Cycles labeled for, while, do-while, and |
| Purpose | Skip the current iteration and move on to the next |
| Alternative | Control can also be achieved with if-else structures |
| Advantage | Increases code readability, avoids unnecessary processing |
| Disadvantage | Control flow can become complicated with overuse |
❓ Frequently Asked Questions (FAQ)
1. What is the difference between continue and break? continue skips the rest of the loop, but the loop continues. break terminates the cycle completely.
2. Can I use if-else instead of continue? In simple cases yes, but continue simplifies the code and reduces complexity.
3. Labeled continue What does it do? It allows skipping the current step of the outer loop when a condition in the inner loop is met.
4. Does it work differently in the do-while loop? No, continue works the same way here; Returns the condition directly.
🎯 Result
The continue statement is a powerful tool for managing flow in loops. It simplifies code and improves performance in complex conditions. However, it should be used with caution as overuse can reduce readability.
☁️ You can try it on the GenixNode platform to test loop optimizations and perform performance analysis.

