- 1Java Exceptions
- 2Java Keywords
- 3Java abstract Keyword
- 4Java assert Keyword
- 5Java boolean Keyword
- 6Java break Keyword
- 7Java byte Keyword
- 8Java case Keyword
- 9Java catch Keyword
- 10Java char Keyword
- 11Java class Keyword
- 12Java const Keyword
- 13Java continue Keyword
- 14Java default Keyword
- 15Java do Keyword
- 16Java double Keyword
- 17Java else Keyword
- 18Java enum Keyword
- 19Java extends Keyword
- 20Java final Keyword
- 21Java finally Keyword
- 22Java float Keyword
- 23Java for Keyword
- 24Java goto Keyword
- 25Java if Keyword
- 26Java implements Keyword
- 27Java import Keyword
- 28Java instanceof Keyword
- 29Java int Keyword
- 30Java interface Keyword
- 31Java long Keyword
- 32Java native Keyword
- 33Java new Keyword
- 34Java null Keyword
- 35Java package Keyword
- 36Java private Keyword
- 37Java protected Keyword
- 38Java public Keyword
- 39Java return Keyword
- 40Java short Keyword
- 41Java static Keyword
- 42Java strictfp Keyword
- 43Java super Keyword
- 44Java switch Keyword
- 45Java synchronized Keyword
- 46Java this Keyword
- 47Java transient Keyword
- 48Java try Keyword
- 49Java void Keyword
- 50Java volatile Keyword
- 51Java while Keyword
- 52Java String Methods - Syntax and Description
- 53Java String
charAt()
method - 54Java String
codePointAt()
method - 55Java String
codePointBefore()
method - 56Java String
codePointCount()
method - 57Java String
compareTo()
method - 58Java String
compareToIgnoreCase()
method - 59Java String
concat()
method - 60Java String
contains()
method - 61Java String
contentEquals()
method - 62Java String
copyValueOf()
method - 63Java String
endsWith()
method - 64Java String
equals()
method - 65Java String
equalsIgnoreCase()
method - 66Java String
format()
method - 67Java String
getBytes()
method - 68Java String
getChars()
method - 69Java String
hashCode()
method - 70Java String
indexOf()
method - 71Java String
intern()
method - 72Java String
isEmpty()
method - 73Java String
join()
method - 74Java String
lastIndexOf()
method - 75Java String
length()
method - 76Java String
matches()
method - 77Java String
offsetByCodePoints()
method - 78Java String
regionMatches()
method - 79Java String
replace()
method - 80Java String
replaceAll()
method - 81Java String
replaceFirst()
method - 82Java String
split()
method - 83Java String
startsWith()
method - 84Java String
subSequence()
method - 85Java String
substring()
method - 86Java String
toCharArray()
method - 87Java String
toLowerCase()
method - 88Java String
toString()
method - 89Java String
toUpperCase()
method - 90Java String
trim()
method - 91Java String
valueOf()
method - 92Java ArrayList Methods - Complete Reference with Syntax and Description
- 93Java LinkedList Methods - Complete Reference with Syntax and Description
- 94Java HashMap Methods - Syntax and Descriptions
Java continue Keyword
Usage and Examples
continue
Keyword in Java
The continue
keyword in Java is used to skip the current iteration of a loop and jump to the next one. It gives you finer control over the flow of loops, especially when certain conditions should interrupt the current cycle without exiting the loop entirely.
Why Use continue
?
Imagine you’re looping over a list of numbers, but want to skip processing any negative values. Or perhaps you're filtering strings and want to avoid ones that are empty. The continue
keyword makes your code cleaner and avoids deeply nested if-statements.
Syntax
continue;
That’s it. The keyword stands alone and ends with a semicolon. It’s typically placed inside an if
condition inside loops.
How continue
Works in a Loop
When the Java runtime encounters continue
, it immediately skips to the next iteration of the nearest enclosing loop. Any code written after continue;
in the current iteration is not executed.
Example 1: Skipping Even Numbers in a for
Loop
public class ContinueExample1 {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
continue; // Skip even numbers
}
System.out.println("Odd number: " + i);
}
}
}
Odd number: 1
Odd number: 3
Odd number: 5
Odd number: 7
Odd number: 9
Explanation
Whenever i % 2 == 0
evaluates to true (i.e., the number is even), the continue
statement is triggered. This skips the System.out.println()
call and moves on to the next iteration.
Example 2: continue
in a while
Loop
public class ContinueExample2 {
public static void main(String[] args) {
int i = 0;
while (i < 10) {
i++;
if (i == 5) {
continue; // Skip when i is 5
}
System.out.println("Value: " + i);
}
}
}
Value: 1
Value: 2
Value: 3
Value: 4
Value: 6
Value: 7
Value: 8
Value: 9
Value: 10
Explanation
When i
becomes 5, continue
is executed. The System.out.println()
line is skipped for that specific iteration.
Example 3: continue
with a for-each
Loop
public class ContinueExample3 {
public static void main(String[] args) {
String[] fruits = {"Apple", "", "Banana", "Mango", ""};
for (String fruit : fruits) {
if (fruit.isEmpty()) {
continue; // Skip empty strings
}
System.out.println("Fruit: " + fruit);
}
}
}
Fruit: Apple
Fruit: Banana
Fruit: Mango
Explanation
We use continue
to skip printing any empty strings. This pattern is useful in data cleansing or filtering scenarios.
Common Mistakes with continue
- Infinite Loops: If used carelessly in
while
ordo-while
loops,continue
can cause the loop condition never to be updated. Always ensure variables are updated before thecontinue
. - Unreachable Code: Placing code after a
continue
statement inside a block will make that code unreachable for that execution path.
When to Use continue
— Best Practices
The continue
keyword is ideal when:
- You want to filter or skip elements conditionally inside a loop.
- You aim to make your loop logic more readable by avoiding deeply nested conditions.
- You want to enforce early exits from unnecessary computations in a loop cycle.