×

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

How to check data type in Java

In this section, we will learn about the methodology to verify the data type in Java. There are mainly two methods in java called as getClass() and getName(). They are...

3 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.

Majority Element in Java

It's an extremely intriguing question that is commonly asked in job interviews at prestigious IT firms like The Google, Amazon, TCS, and The Accenture, etc. By figuring out the solution, one may...

10 minutes read.

Convert IP to Binary in Java

Fundamental conversion, such as going from binary to decimal or vice versa, is a crucial activity in computers. Understanding IP addressing and subnetting is crucial for networking. The primary networking...

4 minutes read.

Java Boolean getBoolean() Method

The getBoolean() method of Java Boolean class returns true if the specified system property is not null and is equal to the String ‘true’, else the result returned is false. Syntax public...

1 minute read.

Java throw

Java throw Sometimes it is required in the code to throw an exception deliberately. In order to achieve the same, the Java throw keyword should be used. One can throw either...

3 minutes read.

Java Generics Questions

Introduction We'll walk through a few real-world examples of interview questions and responses for Java generics in this article. Java 5 saw the debut of the fundamental idea of generics. Due to...

9 minutes read.

Flag Pattern in Java

The flag pattern in Java can be printed, which will be covered in this part. Given how difficult they are to code, flag patterns are rarely asked by interviewers. We separate...

2 minutes read.

InputMismatchException in Java

What is InputMismatchException? One of the most frequent errors in Java is the InputMismatchException. Because the InputMismatchException is a subtype of the java.lang, it is an unchecked exception. RuntimeException.Because it is...

4 minutes read.

Java logger

Logging is a crucial Java feature that aids developers in identifying issues. The logging methodology is included with Java as the programming language. It offers the Logging API, which debuted...

7 minutes read.

Difference Between replace() and replaceall() in Java

In this article, you will be acknowledged about the replace() and replaceall() methods in java. Also, most importantly you will be acknowledged the differences between them along with the example...

4 minutes read.

How to initialize string array in Java

In this tutorial, we will learn how the string array is initialized in java. The initialization of string array in the java by using the NEW (it creates the object for...

3 minutes read.

How to take String Input in Java

There are various ways to take String input in Java. In this section, we are going to discuss how to take String input in Java. There are following ways to...

5 minutes read.

Blockchain in Java

Blockchain is a continuously expanding ledger that maintains an immutable, secure, and chronological record of all transactions that have ever occurred. It can be utilized to securely transfer money, assets,...

9 minutes read.

Example of Static import in Java

Static keyword Java's static keyword is mainly used to control memory. Variables, methods, blocks, and nested classes are compatible with the static keyword. Instead of an instance, the static keyword is...

3 minutes read.

Pattern Programs in Java

Pattern Programs in Java In Java, pattern programs are the most important from the perspective of interviews. The pattern programs improve thinking and coding skills. It also helps us to develop a...

27 minutes read.

How to check the Java version in cmd

To make programs that can run on our systems, we need to install programming language-related software in our systems. Different programming languages require different types of software, aka IDEs (Integrated...

5 minutes read.

Java Integer max() method

The max() method of Integer class returns the greater of two int values. It returns the same result as by calling Math.max. Syntax public static int max(int a, int b) Parameters The parameters ‘a’...

2 minutes read.

Java Constant

A constant is an unchangeable entity in coding, as its title implies. The value which cannot be altered, in other terms. We shall understand about Java constants and exactly how...

3 minutes read.

Binary Strings Without Consecutive Ones in Java

N, an integer, is provided. Our objective is to determine the overall number of strings with length N that do not include successive 1s. Example: Input: 3 Output: 6 Explanation The binary forms of length...

6 minutes read.