Java - Continue Statement
Java continue statement is a branching statement in a control flow statement and it will terminate the iteration of a loop
even though the looping expression is valid (i.e., true). There are two types of continue Statement.
- Continue Statement
- Labeled Continue Statement
continue Statement
The continue statement is a termination statement and its used to terminate the current iteration of the
innermost loop (while / do-while / for)
even though the expression is valid (i.e., true). This is mainly used when you want to continue the loop
but do not want to execute the rest of the statements (after continue statement) of the current iteration.
Labeled continue Statement
The Labeled continue statement is a termination statement and its used to terminate the current iteration of the
outermost loop (while / do-while / for)
even though the expression is valid (i.e., true). This is mainly used when you want to continue the loop
but do not want to execute the rest of the statements (after labeled continue statement) of the current iteration.
Moreover the labeled continue statement will be used commonly in the nested loops to continue the outermost loop.
Example: #1 - while Loop with continue Statement
Print all the odd numbers from the 1-dimensional array and terminate the current iteration of do-while loop using simple continue statement only if the value is even number.
Java Input Screen
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]);
}
}
}
Example: #2 - Nested while Loop with continue Statement
Find each row contains an ODD number in a 2-dimensional array using simple continue statement in do-while loop.
But the following program will print the duplicate message on each occurance of an ODD number in a row instead of a single generic message.
Java Input Screen
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));
}
}
}
}
Java Output Screen
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
Thousands Lived without Love, but not without water. So SAVE WATER.
Example: #3 - Nested while Loop with Labeled continue Statement
Find each row contains an ODD number in a 2-dimensional array using labeled continue statement in do-while loop.
Example #2 prints the duplicate message for all the occurance of an ODD number in a row but the following example will print only one message per row eventhough the ODD number exists multiple time.
Java Input Screen
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));
}
}
}
Java Output Screen
Odd number not found in row 1
Odd number found in row 2
Odd number found in row 3
Thousands Lived without Love, but not without water. So SAVE WATER.
Example: #4 - do-while Loop with continue Statement
Print all the odd numbers from the 1-dimensional array and terminate the current iteration of do-while loop using simple continue statement only if the value is even number.
Java Input Screen
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);
}
}
Thousands Lived without Love, but not without water. So SAVE WATER.
Example: #5 - Nested do-while Loop with continue Statement
Find each row contains an ODD number in a 2-dimensional array using simple continue statement in do-while loop.
But the following program will print the duplicate message on each occurance of an ODD number in a row instead of a single generic message.
Java Input Screen
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);
}
}
Java Output Screen
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
Thousands Lived without Love, but not without water. So SAVE WATER.
Example: #6 - Nested do-while Loop with Labeled continue Statement
Find each row contains an ODD number in a 2-dimensional array using labeled continue statement in do-while loop.
Example #5 prints the duplicate message for all the occurance of an ODD number in a row but the following example will print only one message per row eventhough the ODD number exists multiple time.
Java Input Screen
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);
}
}
Java Output Screen
Odd number not found in row 1
Odd number found in row 2
Odd number found in row 3
Example: #7 - for Loop with continue Statement
Print all the odd numbers from the 1-dimensional array and terminate the current iteration of
for loop using simple continue statement only if the value is even number.
Java Input Screen
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]);
}
}
}
Thousands Lived without Love, but not without water. So SAVE WATER.
Example: #8 - Nested for Loop with continue Statement
Find each row contains an ODD number in a 2-dimensional array using simple continue statement in for loop.
But the following program will print the duplicate message on each occurance of an ODD number in a row instead of a single generic message.
Java Input Screen
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));
}
}
}
}
Java Output Screen
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
Thousands Lived without Love, but not without water. So SAVE WATER.
Example: #9 - Nested for Loop with Labeled continue Statement
Find each row contains an ODD number in a 2-dimensional array using labeled continue statement in for loop.
Example #8 prints the duplicate message for all the occurance of an ODD number in a row but the following example will print only one message per row eventhough the ODD number exists multiple time.
Java Input Screen
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));
}
}
}
Java Output Screen
Odd number found in row 2
Odd number found in row 3
BBMINFO