continue Statement
An unlabeled continue applies to the innermost enclosing loop.
In Java, continue skips the rest of the current loop iteration. Execution moves to the loop update or condition check.
Skip just the number 2
class Main {
public static void main(String[] args) throws Exception {
for (int n = 1; n <= 3; n++) {
if (n == 2) {
continue;
}
System.out.println(n);
}
}
}
1 3
A counter records visitors in each time interval. A negative reading signals a fault, while zero means the counter worked and nobody entered. Keeping 0, 4 and 6 gives three valid intervals and ten visitors.
An unlabeled continue applies to the innermost enclosing loop.
A labeled continue applies to the enclosing loop that carries the specified label.
Skip even values and print the odd values in the array.
class ControlFlowStatement {
public static void main(String[] args) {
// Declare and Initialize the 1-dim integer array
int[] arr = {2,5,6};
// Declare and Initialize the row index variable
int index = -1;
// The 1-dim array is 1x3 matrix so the array's index should be 0,1,2
while(index < 2) {
// Increment the index by 1 using Post Increment
index++;
// Terminate the current iteration only if the number is even
if(arr[index] % 2 == 0) {
continue;
}
System.out.println(arr[index]);
}
}
}
5
Trace the inner loop to see which elements are skipped and when the next row begins.
class ControlFlowStatement {
public static void main(String[] args) {
// Declare and Initialize the 2-dim integer array
int[][] arr = {
{2,4,6},
{5,10,15},
{9,18,27}
};
// Declare and Initialize the row index variable
int row = -1;
// The 2-dim array is 3x3 matrix so the array's row index should be 0,1,2
while(row < 2) {
// Increment the row index by 1 using Post Increment
row++;
// Declare and Initialize the column index variable
int column = -1;
boolean result = false;
// The 2-dim array is 3x3 matrix so the array's column index should be 0,1,2
while(column < 2) {
// Increment the column index by 1 using Post Increment
column++;
// Terminate the current itteration only if the value is even
if(arr[row][column] % 2 == 0) {
continue;
}
result = true;
System.out.println("Odd number found in row " + (row + 1));
}
if(result == false) {
System.out.println("Odd number not found in row " + (row + 1));
}
}
}
}
Odd number not found in row 1 Odd number found in row 2 Odd number found in row 2 Odd number found in row 3 Odd number found in row 3
Continue the labeled outer loop when the row contains an odd number.
class ControlFlowStatement {
public static void main(String[] args) {
// Declare and Initialize the 2-dim integer array
int[][] arr = {
{2,4,6},
{5,10,15},
{9,18,27}
};
// Declare and Initialize the row index variable
int row = -1;
// Label
outer:
// The 2-dim array is 3x3 matrix so the array's row index should be 0,1,2
while(row < 2) {
// Increment the row index by 1 using Post Increment
row++;
// Declare and Initialize the column index variable
int column = -1;
// The 2-dim array is 3x3 matrix so the array's column index should be 0,1,2
while(column < 2) {
// Increment the column index by 1 using Post Increment
column++;
// Once matching found update the result flag to true
// and terminate the current execution
if(arr[row][column] % 2 == 1) {
System.out.println("Odd number found in row " + (row + 1));
continue outer;
}
}
System.out.println("Odd number not found in row " + (row + 1));
}
}
}
Odd number not found in row 1 Odd number found in row 2 Odd number found in row 3
Skip even values and print the odd values in the array.
class ControlFlowStatement {
public static void main(String[] args) {
// Declare and Initialize the 1-dim integer array
int[] arr = {2,5,6};
// Declare and Initialize the row index variable
int index = -1;
// The 1-dim array is 1x3 matrix so the array's index should be 0,1,2
do {
// Increment the index by 1 using Post Increment
index++;
// Terminate the current iteration only if the number is even
if(arr[index] % 2 == 0) {
continue;
}
System.out.println(arr[index]);
} while(index < 2);
}
}
5
Trace the inner loop to see which elements are skipped and when the next row begins.
class ControlFlowStatement {
public static void main(String[] args) {
// Declare and Initialize the 2-dim integer array
int[][] arr = {
{2,4,6},
{5,10,15},
{9,18,27}
};
// Declare and Initialize the row index variable
int row = -1;
// The 2-dim array is 3x3 matrix so the array's row index should be 0,1,2
do {
// Increment the row index by 1 using Post Increment
row++;
// Declare and Initialize the column index variable
int column = -1;
boolean result = false;
// The 2-dim array is 3x3 matrix so the array's column index should be 0,1,2
do {
// Increment the column index by 1 using Post Increment
column++;
// Terminate the current itteration only if the value is even
if(arr[row][column] % 2 == 0) {
continue;
}
result = true;
System.out.println("Odd number found in row " + (row + 1));
} while(column < 2);
if(result == false) {
System.out.println("Odd number not found in row " + (row + 1));
}
} while(row < 2);
}
}
Odd number not found in row 1 Odd number found in row 2 Odd number found in row 2 Odd number found in row 3 Odd number found in row 3
Check how the loop advances its index before continuing to the next row.
class ControlFlowStatement {
public static void main(String[] args) {
// Declare and Initialize the 2-dim integer array
int[][] arr = {
{2,4,6},
{5,10,15},
{9,18,27}
};
// Declare and Initialize the row index variable
int row = -1;
// Label
outer:
// The 2-dim array is 3x3 matrix so the array's row index should be 0,1,2
do {
// Increment the row index by 1 using Post Increment
row++;
// Declare and Initialize the column index variable
int column = -1;
// The 2-dim array is 3x3 matrix so the array's column index should be 0,1,2
do {
// Increment the column index by 1 using Post Increment
column++;
// Once matching found update the result flag to true
// and terminate the current execution
if(arr[row][column] % 2 == 1) {
System.out.println("Odd number found in row " + (row + 1));
continue outer;
}
} while(column < 2);
System.out.println("Odd number not found in row " + (row + 1));
} while(row < 2);
}
}
Odd number not found in row 1 Odd number found in row 2 Odd number found in row 3
Use continue in a for loop to skip even values.
class ControlFlowStatement {
public static void main(String[] args) {
// Declare and Initialize the 1-dim integer array
int[] arr = {2,5,6};
// The 1-dim array is 1x3 matrix so the array's index should be 0,1,2
for(int index = 0; index < 3; index++) {
// Terminate the current iteration only if the number is even
if(arr[index] % 2 == 0) {
continue;
}
System.out.println(arr[index]);
}
}
}
5
Trace which loop receives the unlabeled continue when an odd value is found.
class ControlFlowStatement {
public static void main(String[] args) {
// Declare and Initialize the 2-dim integer array
int[][] arr = {
{2,4,6},
{5,10,15},
{9,18,27}
};
// The 2-dim array is 3x3 matrix so the array's row index should be 0,1,2
for(int row = 0; row < 3; row++) {
boolean result = false;
// The 2-dim array is 3x3 matrix so the array's column index should be 0,1,2
for(int column = 0; column < 3; column++) {
// Terminate the current itteration only if the value is even
if(arr[row][column] % 2 == 0) {
continue;
}
result = true;
System.out.println("Odd number found in row " + (row + 1));
}
if(result == false) {
System.out.println("Odd number not found in row " + (row + 1));
}
}
}
}
Odd number not found in row 1 Odd number found in row 2 Odd number found in row 2 Odd number found in row 3 Odd number found in row 3
Use a labeled continue to advance directly to the next row.
class ControlFlowStatement {
public static void main(String[] args) {
// Declare and Initialize the 2-dim integer array
int[][] arr = {
{2,4,6},
{5,10,15},
{9,18,27}
};
// Label
outer:
// The 2-dim array is 3x3 matrix so the array's row index should be 0,1,2
for(int row = 0; row < 3; row++) {
// The 2-dim array is 3x3 matrix so the array's column index should be 0,1,2
for(int column = 0; column < 3; column++) {
// Terminate the current itteration only if the value is even
if(arr[row][column] % 2 == 0) {
continue outer;
}
System.out.println("Odd number found in row " + (row + 1));
}
System.out.println("Odd number not found in row " + (row + 1));
}
}
}
Odd number found in row 2 Odd number found in row 3
Reference: Java language reference