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