Continue Statement in Java with Examples

The continue statement in Java is a loop control statement used to skip the current iteration of a loop and move the control to the next iteration. It is widely used in loops to skip certain iterations based on conditions.

Whenever the continue statement is encountered inside a loop:

  • The remaining statements inside the loop for the current iteration are skipped based on certain condition.
  • The control immediately jumps to the next iteration by skipping the current iteration.
  • The loop condition is checked again.
Learn continue statement in Java with syntax and basic to advanced examples.

Why Use Continue Statement in Java?

The continue statement is useful in situations where:

  • You want to skip unwanted values in a loop.
  • You want to reduce nested conditions.
  • You want to improve readability of code.
  • You want to avoid executing certain statements under specific conditions.

Syntax of Continue Statement in Java

The basic syntax of continue statement without label in Java is:

continue;

This statement skips the current iteration of the loop and continues with the next iteration.

The basic syntax of continue statement with label is as:

continue labelName;

This statement skips the current iteration of a labeled loop and moves control to the next iteration of that specific loop.

How Continue Statement Works in Java

The continue statement works only inside loops such as:

  • for loop
  • while loop
  • do-while loop

When the continue statement is encountered inside a loop, it skips the remaining statements of the current iteration and transfers control to the next iteration of the loop. In a for loop, it moves to the update expression before condition checking, while in while and do-while loops, it directly jumps to condition evaluation.

Basic Example of Continue Statement in Java

Let us take some basic examples based on continue statement in Java that you must practice them.

Example 1: Skipping a Number in For Loop

public class ContinueEx1 {
public static void main(String[] args) {
for(int i = 1; i <= 5; i++) {
if(i == 3) {
continue;
}
System.out.println(i);
}
}
}

Output:

1
2
4
5

In this example, when the value of i becomes 3, the continue statement inside the if block executes and it skips the current iteration for i = 3. After skipping, the loop continues from the next iteration for i = 4 and i = 5.

Example 2: Skipping Even Numbers

public class ContinueEx2 {
public static void main(String[] args) {
int i = 1;
while(i <= 10) {
i++;
if(i % 2 == 0) {
continue;
}
System.out.println(i);
}
}
}

Output:

3
5
7
9
11

In this example, when the value of i is even, continue statement inside the if block skips the current iteration. Therefore, only odd numbers are displayed on the console.

Example 3: Skipping Multiples of 3

public class ContinueEx3 {
public static void main(String[] args) {
int i = 1;
do {
if(i % 3 == 0) {
i++;
continue;
}
System.out.println(i);
i++;
} while(i <= 10);
}
}

Output:

1
2
4
5
7
8
10

Example 4: Continue Statement inside Nested Loop

When the continue statement is used inside nested loops, it affects only the innermost loop, not the outermost loop.

public class ContinueEx4 {
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);
}
}
}
}

Output:

i = 1 j = 1
i = 1 j = 3
i = 2 j = 1
i = 2 j = 3
i = 3 j = 1
i = 3 j = 3

In this example, whenever the value of j equals to 2, the continue statement inside nested loop skips that iteration. However, the outer loop remains unaffected.

Example 5: Labeled Continue Statement

Labeled continue statement allows you to skip iterations of outer loops in nested structures.

public class ContinueExample5 {
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);
}
}
}
}

Output:

i = 1 j = 1
i = 2 j = 1
i = 3 j = 1

Real-Life Example of Continue Statement

Let us take an real-time example based on the continue statement in Java in which we will skip invalid marks of student. Look at the example code below to understand better.

public class StudentMarks {
public static void main(String[] args) {
int marks[] = {85, -1, 76, 92, -5, 88};
for(int m : marks) {
if(m < 0) {
continue;
}
System.out.println("Valid Marks: " + m);
}
}
}

Output:

Valid Marks: 85
Valid Marks: 76
Valid Marks: 92
Valid Marks: 88

Difference Between Break and Continue Statement in Java

Both break and continue statements are loop control statements in Java. They are used to alter the normal flow of loop execution, but they work in different ways. You must undestand the basic difference between them.

Break Statement in Java

The break statement is used to terminate the loop completely. When the break statement is encountered inside a loop, the control immediately exits the loop and moves to the next statement after the loop.

Continue Statement in Java

The continue statement is used to skip the current iteration of a loop. Instead of terminating the loop, it skips the remaining statements of the current iteration and moves to the next iteration.

Key Differences Between Break and Continue

FeatureBreak StatementContinue Statement
PurposeTerminates the loop completely.Skips current iteration only.
Loop ExecutionStops entire loop execution.Continues loop execution.
Control TransferMoves control outside the loop.Moves control to next iteration.
UsageUsed when loop must stop early.Used when some values must be skipped.
Works in SwitchYesNo
Nested Loop BehaviorTerminates the current loop.Skips iteration of current loop.
Program FlowExits loop immediately.Continues loop with next cycle.

Please Share Your Love.

Leave a Reply

Your email address will not be published. Required fields are marked *