The general form of simple continue statement is:
continue;
The unlabeled continue statement can be used inside loops (for, while and do-while) only. It prematurely stops the current iteration of the loop body and proceeds with the next iteration, if possible.
In the case of while and do-while loops, the rest of the body is skipped and the execution continues with the loop condition. In the case of for loop, the rest of the body is skipped and the execution continues with the increment/decrement expression.
This is similar to continue statement in C/C++.
Example: The linear search program is rewritten to make use of the continue statement.
import java.util.*;
class LinearSearch
{ public static void main(String args[]) throws IOException
{ int n, x, a[]; boolean found = false; String number;
Buffered Reader in
= new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the number of elements in the array: ");
number = in.readLine(); n = Integer.parseInt(number);
a = new int[n];
for(int i = 0; i < n; i++)// read the array elements
{ number = in.readLine();
a[i] = Integer.parseInt(number);
}
System.out.print("Enter the element to be searched in the array: ");
number = in.readLine(); x = Integer.parseInt(number);
int i = 0;
while(i < n && !found) // search the specified element
{
if(x = = a[i]) // simple if statement
{
found = true;
continue;
}
i++;
}
if(found) // if-else statement
System.out.println("The number " + x + " is present in the list");
else
System.out.println("The number " + x + " is not in the list");
}
}
labeled continue Statement
The syntax of the labeled continue statement is as follows:
continue <label>;
The labeled continue statement specifies the label of the enclosing loop to continue. The label need not correspond to the closest enclosing loop.
Example: The matrix search program is rewritten here to make use of the continue statement.
import java.util.*;
class SearchMatrix1
{ public static void main(String args[]) throws IOException
{
int i, j, x, row, col , matrix[][]; String number; boolean found = false;
Buffered Reader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the number of rows in the matrix: ");
number = in.readLine(); row = Integer.parseInt(number);
System.out.print("Enter the number of columns in the matrix: ");
number = in.readLine(); col = Integer.parseInt(number);
matrix = new int[row][col];
for(i = 0; i < row; i++) //enter one element in one line
{
System.out.println("enter the elements of row " + (i+1));
for(j = 0; j < col; j++)
{ number = in.readLine();
}
}
System.out.print("Enter the element to be searched in the matrix: ");
number = in.readLine();
x = Integer.parseInt(number);
outerloop: for(i = 0; i < row && !found; i++)
{ for(j = 0; j < col; j++)
{ if(matrix[i][j] == x)
{ found = true;
continue outerloop;
}
}
}
// break will transfer control to the following statement
if(found)
System.out.println("The number "+ x + " is present in the matrix");
else
System.out.println("The number " + x + " is not in the matrix");
}
}
continue;
The unlabeled continue statement can be used inside loops (for, while and do-while) only. It prematurely stops the current iteration of the loop body and proceeds with the next iteration, if possible.
In the case of while and do-while loops, the rest of the body is skipped and the execution continues with the loop condition. In the case of for loop, the rest of the body is skipped and the execution continues with the increment/decrement expression.
This is similar to continue statement in C/C++.
Example: The linear search program is rewritten to make use of the continue statement.
import java.util.*;
class LinearSearch
{ public static void main(String args[]) throws IOException
{ int n, x, a[]; boolean found = false; String number;
Buffered Reader in
= new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the number of elements in the array: ");
number = in.readLine(); n = Integer.parseInt(number);
a = new int[n];
for(int i = 0; i < n; i++)// read the array elements
{ number = in.readLine();
a[i] = Integer.parseInt(number);
}
System.out.print("Enter the element to be searched in the array: ");
number = in.readLine(); x = Integer.parseInt(number);
int i = 0;
while(i < n && !found) // search the specified element
{
if(x = = a[i]) // simple if statement
{
found = true;
continue;
}
i++;
}
if(found) // if-else statement
System.out.println("The number " + x + " is present in the list");
else
System.out.println("The number " + x + " is not in the list");
}
}
labeled continue Statement
The syntax of the labeled continue statement is as follows:
continue <label>;
The labeled continue statement specifies the label of the enclosing loop to continue. The label need not correspond to the closest enclosing loop.
Example: The matrix search program is rewritten here to make use of the continue statement.
import java.util.*;
class SearchMatrix1
{ public static void main(String args[]) throws IOException
{
int i, j, x, row, col , matrix[][]; String number; boolean found = false;
Buffered Reader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the number of rows in the matrix: ");
number = in.readLine(); row = Integer.parseInt(number);
System.out.print("Enter the number of columns in the matrix: ");
number = in.readLine(); col = Integer.parseInt(number);
matrix = new int[row][col];
for(i = 0; i < row; i++) //enter one element in one line
{
System.out.println("enter the elements of row " + (i+1));
for(j = 0; j < col; j++)
{ number = in.readLine();
}
}
System.out.print("Enter the element to be searched in the matrix: ");
number = in.readLine();
x = Integer.parseInt(number);
outerloop: for(i = 0; i < row && !found; i++)
{ for(j = 0; j < col; j++)
{ if(matrix[i][j] == x)
{ found = true;
continue outerloop;
}
}
}
// break will transfer control to the following statement
if(found)
System.out.println("The number "+ x + " is present in the matrix");
else
System.out.println("The number " + x + " is not in the matrix");
}
}