×

Java Continue Keyword

  • The java keyword ‘continues’ has also been called as java continue statement.
  • The main concepts of the java continue statement or keyword is used in the controlling of the loop structure.
  • Java continue statement is implemented when we have a need of jumping or escaping idea into next iteration of the loop faster or in urgency.
  • In inside the loops, when continue statement is executed or arrived then the control of the loop directly jump to the initial statements of the loop for the coming iteration instead of running the statements of current iteration.
  • The java continues statement or keyword can be defined with the loops such as for loop and while loop.
  • The main aspects of the java continue keyword is to continue the loop in a program.
  • It escapes the code at a particular given conditions and continues the current flow of the program by continuing the flow execution of the loops.
  • For example, if we implement the continue statement in case of the inner loops then its continues the inner loop only in the program.
  • The implementation of the continue statement or keyword is done with the various types of the loops like for loop, while loop, do-while loop.
  • In usage of the for loop, the continue keyword or statement force control to jump immediately to the update statements.
  • In case of implementation of the while loop or do-while loop, flow control immediately escapes to the next iteration.

Syntax of continue statements in java

Continue.

Examples of Continue Keyword

Example1: Simple Example for java continue keyword

ContEx.java
//continue statement or keyword in for loop.  
public class ContEx {  
public static void main (String [] args) {  
    //for loop  
for (int z=10; z<=20; z++) {
        if(z==15) {
            //Here we are implemeting continue statement for skipping 
            continue;//continue keyword will skip or escapes the remaining statement  
        }  
        System.out.println(z);
    }  
}  
}  

Output:

10
11
12
13
14
16
17
18
19
20

Here in the above given output, 15 is not executed or printed on the console because we skipped it using the continue statement. That is why the loop is runned or executed when it comes to 15.

Example2: Java Continue Statement with implementation of the Inner Loop

Implementing the continue statement inside the inner loop of the program.

ContEx2.java


//code to define the use of continue statement in inner loop of the program
//implemetation of the inner loop  
public class ContEx2 {  
public static void main (String [] args) {  
for (int k=5; k<=7; k++) {
for (int m=5; m<=7; m++) {
                        if(k==6&&m==6) {
                            //using continue statement or keyword within the inside inner loop  
                            continue;    
                        }    
                        System.out.println(k+" "+m);
                    }    
            }    
}  
}  

Output:

5 5
5 6
57
65
67
75
76
7 7

Example3:

The continue statement can be implemented with a label. This feature is defined since JDK 1.5. So, the continue statement can be used in any loop in Java either it is outer loop or inner loop.

ContinueEx3.java
//with label inside an inner loop to continue outer loop  
public class ContEx3 {  
public static void main (String [] args) {  
LL:  
for (int q=10; q<=13; q++) {
MM:  
for (int r=10; r<=13; r++) {
                        if(iq==11&&r==11) {
                            continue LL;
                        }    
                        System.out.println(q+" "+r);
                    }    
            }    
}  
}  

Output:

10  10
1011
1013
11 10
13 10
1311
13 13

Example4: Java Continue Statement using the while loop

ConWhileEx.java


//implementing the continue keyword inside the while loop.  
public class ConWhileEx {  
public static void main (String [] args) {  
    //initalizing the while loop  
    int p=1;  
    while(p<=10) {
        if(p==5) {
            //implementing continue statement  
p++;
continue;
        }  
        System.out.println(p);
p++;
    }  
}  
}  

Output:

1
2
3
4
6
7
8
9
10

Example5: Java Continue Statement using the do-while Loop

ConDoWhileEx.java
//initilizing the continue inside the Java do-while loop.  
public class ConDoWhileEx {  
public static void main (String [] args) {  
    //declaring variable  
    int u=10;
    //initializing do-while loop  
do {
        if(u==17) {
                //using continue statement  
u++;
            continue;//skipping the remaining statements
        }  
        System.out.println(u);
u++;
} while(u<=20);
}  
}  

Output:

10
11
12
13
14
15
16
18
19
20

Related Topics

Exception Handling in Java

Before looking at how exceptions are handled in java, it is necessary to see what an exception is. What is Exception? Whenever a program is written, errors are encountered. Some of these...

6 minutes read.

Java String contains() method

contains() method returns true only if it contains the given sequence of characters otherwise it returns false. syntax: public boolean contains(CharSequence sequence) parameters: sequence : It is the sequence to be searched. Returns: It returns true...

1 minute read.

Matrix Multiplication in Java

In Java, using the binary operator (*) we can perform matrix multiplication. A Matrix is a group of arrays. In the multiplication of matrices, the elements of each row are...

3 minutes read.

How to Calculate the Time Difference between Two Dates in Java?

The date is used extensively in Java to calculate date discrepancies. The date of joining an organization, admittance, appointment, etc., can be included when creating the application. The differences between...

4 minutes read.

Java.sql.Time Format

In JDBC API as a cover aroundjava.util.Date that handles SQL-specific requirements we use the java.sql.Time. The java.sql.Time extends java.util.Date class. To represent SQL TIME, without a date this class is...

6 minutes read.

Balanced Parentheses in Java

One of the frequent programming issues, commonly referred to as the Balanced brackets issue, is the problem with the balanced parenthesis. Interviewers frequently provide this task, in which we must...

5 minutes read.

Java Class Methods

Methods called on a class rather than a specific object instance are known as class methods. The static modifier guarantees uniform implementation across all instances of the class. Syntax public class NameOfClass...

3 minutes read.

Java String trim() method

Java String trim() method returns String after eliminating leading and trailing spaces. Note:- Java String trim() method doesn't trim or omit middle spaces. Syntax: public String trim() Returns: It returns string with omitted leading and...

2 minutes read.

Static Array in Java

In this tutorial, we will study static arrays in Java. An array is a data structure that is of great importance in any programming language. It is classified into two...

3 minutes read.

Java Math toDegrees() Method

The toDegrees() method of Java Math class converts a radian angle to an approximately equivalent angle measured in degrees. Syntax: public static double toDegrees(double angrad) Parameters: The parameter ‘angrad ‘represents an angle measured in...

2 minutes read.

Java Database Connectivity with Oracle

JDBC: A Programmer can develop a complete application using the Java built-in API’s. So, for storing the data required for solving a real-world problem is stored into a database. To connect...

5 minutes read.

Java Math addExact() Method

The addExact() method of Math class returns the sum of the two arguments, throwing an exception if the result overflows a long or an int. Syntax public static int addExact (int x,...

1 minute read.

How to Convert Date to String in Java

How to Convert Date to String in Java We need to convert Date to String in Java may for displaying purpose. We can convert Date to String in Java using the format() method of java.text.DateFormat class. There...

2 minutes read.

Java Math cbrt() Method

The cbrt() method of Math class returns the cube root of a double value. Syntax: public static double cbrt(double a) Parameters: The parameter ‘a’ represents the value whose cube root is to be determined. Return...

2 minutes read.

Java Continue Keyword

The java keyword ‘continues’ has also been called as java continue statement.The main concepts of the java continue statement or keyword is used in the controlling of the loop structure.Java...

3 minutes read.

Java Math cosh() Method

The cosh() method of Math class returns the first hyperbolic cosine((e+e)/2) of a double value. Syntax: public static double cosh(double x) Parameters: The parameter ‘x’ represents the number whose hyperbolic cosine is to be...

2 minutes read.

Java Integer compareUnsigned() method

The compareUnsigned() method of Integer class compares two int objects numerically by treating the values as unsigned. Syntax public static int compareUnsigned(int x , int y) Parameters The parameters ‘x’ and ‘y’ represent the...

1 minute read.

Ternary Operator in Java

In some cases, the if...else expression in Java can be replaced by a ternary operator. Visit the Java if...else statement first before learning about the ternary operator. Ternary operator in Java A...

3 minutes read.

Operators in Java

There is a good operator environment provided by Java. These operators are divided into four different groups i.e. arithmetic, bitwise, relational, and logical. There are other operators also available to...

7 minutes read.

Java Binary Tree

The non-linear data structure known as a binary tree is a type of tree, and because it stores data in a hierarchical manner, it is mostly utilised for finding and...

7 minutes read.