×

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

Print Matrix Diagonally in Java

The aim is to print the elements of a matrix of size n*n in some kind of a diagonal pattern. Input : mat[3][3] = {{1, 2, 3},                      {4, 5, 6},                      {7,...

3 minutes read.

Java Image

For all other classes used for representing graphical images, Java's Image class serves as an abstract superclass. For images in Java, a specific form of object known as a BufferedImage...

4 minutes read.

How to Round Double Float up to Two Decimal Places in Java

It indicates 15 digits just after the decimal place in Java whenever a double data type is used in front of a variable. For example, when representing rupees and other...

4 minutes read.

Array and String with Examples in Java

Array in Java: An array in Java is a group of variables with similar types that have a common name. The arrays used in Java differ from those used in C/C++. Key...

6 minutes read.

Java Math negateExact() Method

The negateExact() method of Math class returns the negation for the specified argument, throwing an exception if the result overflows an int or a long. Syntax: public static int negateExact (int a)public...

1 minute read.

Java String

Definition A string is a series of characters. Consider an example, "Welcome" is a 7-character string. In Java, String is an unchangeable object. That is, the String is perpetual and cannot be replaced after it is created. This is the tutorial in which you...

4 minutes read.

Java vs Golang

Java Java is both a programming language and a platform. Java is a high-level, dependable, object-oriented, and secure programming language. Java was developed in 1995 by Sun Microsystems, which is now an...

4 minutes read.

Morris Traversal for Inorder in Java

Through Morris’s traversal, a tree is traversed without the aid of recursion or stacks. Based on the threaded binary tree, the Morris traversal is used. We perform internal modification throughout...

4 minutes read.

Java Control Statements

Control Statements: Control statements in Java can also be referred to as decision-making while dealing with different problems. Control statements are helpful to sort out the flow of the program or...

7 minutes read.

Dining Philosophers problem in Java

The Problem of the Dining Philosophers illustrates a concurrency issue involving the distribution of scarce resources among conflicting processes. Problem Statement Imagine a dining table with a circle in the middle and five...

3 minutes read.

Static class in Java

In Java, a class is a file containing the Java byte code. It can essentially specifically be executed on the JVM (Java Virtual Machine), fairly significant. The JVM is mostly...

7 minutes read.

How to download and install Eclipse in Windows?

Download and Install Eclipse on Windows Eclipse is an open source IDE (Integrated Development Environment) which is used to help the programmers to provide a platform to write and run the...

1 minute read.

Difference between Aggregation and Composition in Java

What is Aggregation? Before we start discussing Aggregation, we have to know some general information about our classes in java. Generally we have two members in our classes , the first...

8 minutes read.

JAR File in Java

What is a JAR File? JAR stands for Java Archive. It is a file format mainly used to combine many Java class files and the corresponding metadata and resources into one...

4 minutes read.

JRE (Java Runtime Environment)

JRE is an installation package that provides an environment to run the Java program on any Operating System. It does not deal with the development process of any application. It is a part...

2 minutes read.

How to Convert String to enum in Java?

In this article, we shall gain the complete knowledge about how to convert the string to enum in Java. The complete process that happens in the approach shall be discussed...

3 minutes read.

Java String endsWith() method

Checks whether this String ends with the specified suffix or not. Syntax public boolean endsWith(String Suffix) Parameter Suffix : It takes suffix as a parameter  i.e. Sequence of characters Returns It returns true when the current...

1 minute read.

Coin change problem in dynamic programming

In this tutorial, we will understand a popular problem called the coin changeproblem through dynamic programming. This problem checks the logical and critical thinking ability of the person. Dynamic Programming...

5 minutes read.

Lazy Propagation in Segment Tree in Java

The topic of segment trees in Java is continued by the topic of sluggish propagation in segment trees. It is suggested that readers first read through the section tree topic....

4 minutes read.

String Palindrome Program in Java

String Palindrome Program in Java The palindrome is a string, phrase, word, number, or other sequences of characters that can be read in both directions i.e. forward (left to right) and...

11 minutes read.