Continue statement

The continue statement does not break a loop, but can be used to skip certain steps based on a condition. So it basically, stops the current step of the loop and moves to the next one, so we can say that this statement continues the loop.

Continue statement can be used with for loop or while loop.

Syntax

jump-statement;

continue; // continue word followed by semi colon.

Example

class ContinueExample {

public static void main(String args[]){

for (int X=1; X<=5; X++)

{

if (X==3)

{

continue;

}

System.out.print(X+” “);

}

}

}

Output

1 2 3 4

Explanation:

In above example we see, value 3 is missing in output because when value of variable X is 3, program bear a continue statement and skip the rest statement.

Flowchart of continue statement
Continue statement
Java continue in while Loop

class ContinueWhileExample {

public static void main(String args[]){

//while loop

int X=10;

{

if (X==4)

//use continue statement

{

X–;

continue; //skip rest statement

}

System.out.print(X+” “);

X–;

}

}

}

Output

10 9 8 7 6 5 3 2 1 0

Explanation:

We are repeating this loop from 10 to 0 for X value. When X value is 4 the loop skipped the print statement and started next repetition of the while loop.

Java continue in do-while Loop

class ContinueDoWhileExample {

public static void main(String args[]){

//variable declare

int X=0;

//do-while loop

do

{

if (X==4)

{

//use continue statement

X++;

continue; //skip rest statement

}

System.oufor (int Y = 0; Y < 3; ++Y) {t.print(X+ " ");

X++;

}while(X<10);

}

}

Output

0 1 2 3 5 6 7 8 9

Java continue with labeled For Loop

The continue statement can be used with labels too.

Example

class ContinueLabeledExample {

public static final int arr[] = {5, 1, 4, 2, 3};

public static void main(String[] args) {

for (int X = 0; X < 3; ++X) {

for (int Y = 0; Y < 3; ++Y) {

for (int Z = 0; Z < 3; ++Z) {

if (Z == 1){

continue;

}

System.out.println(“(X, Y, Z) = (” + X + “,” + Y + “,” + Z + “)”);

}

}

}

}

}

Output

(X, Y, Z) = (0,0,0)

(X, Y, Z) = (0,0,2)

(X, Y, Z) = (0,1,0)

(X, Y, Z) = (0,1,2)

(X, Y, Z) = (0,2,0)

(X, Y, Z) = (0,2,2)

(X, Y, Z) = (1,0,0)

(X, Y, Z) = (1,0,2)

(X, Y, Z) = (1,1,0)

(X, Y, Z) = (1,1,2)

(X, Y, Z) = (1,2,0)

(X, Y, Z) = (1,2,2)

(X, Y, Z) = (2,0,0)

(X, Y, Z) = (2,0,2)

(X, Y, Z) = (2,1,0)

(X, Y, Z) = (2,1,2)

(X, Y, Z) = (2,2,0)

(X, Y, Z) = (2,2,2)

Explanation:

Here in output, to see that what combinations are printed. We clearly notice that no combination with
Z=1 has printed.

Copyright 1999- Ducat Creative, All rights reserved.