Continue Statement in Java comes under the category of branching statements. The other branching statements are break and return statements. Continue is one of the 51 keywords in java. Keywords in java are also known as reserved words that have a specific purpose. These keywords are not supposed to be used as variable names, method names, class names. The purpose of writing a continue statement in java code is to skip the current iteration of a loop, say for, while and do-while. The control is handled mostly to the same loop (if not broken) or passed to the next statement of the code (if the current loop is broken).
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
The continue statement serves the same purpose in case other programming languages such as C and C++. It is also a keyword in C and C++. The continue statement is just opposite to the break statement; if a break statement is encountered, it automatically breaks the loop. Whereas the return statement completely exits from the program. Both return and break are reserved keywords in the case of C, C++, and Java. Neither of them should be used for naming a variable, method or class.
Syntax:
for (i =0; i<max; i++) // for loop is a sample loop, max is the maximum count at which the loop breaks { <Statements> //code statements If (done with this iteration) // if this condition validates to true the continue statement is executed { Continue; // statement itself } <Statements> // code statements }
Below are some examples of the statement in java:
Usage of Continue Statement with for loop.
Code:
public class DemoContinueUsingFor { public static void main(String[] args){ for(int p=0;p<6;p++){ if(p==3){ continue; } System.out.print(p+" "); } } }
Output:
Explanation:
Output:
Usage of Continue Statement with the while loop.
Code:
public class DemoContinueUsingWhile { public static void main(String[] args){ int max = 0; while(max <= 10){ if(max == 6){ max++; continue; } System.out.print(max+" "); max++; } } }
Output:
Explanation:
Code:
public class DemoContinueUsingWhile { public static void main(String[] args){ int max = 0; while(max <= 10){ if(max == 6){ continue; max++; // Here the max ++ is written after continue statement } System.out.println(max+" "); } } }
Explanation:
Output:
Usage of Continue Statement with a do-while loop.
Code:
public class DemoContinueUsingDoWhile { public static void main(String[] args) { int k=10; do { if (k==6) { k--; continue; } System.out.print(k+ " "); k--; } while(k>0); } }
Output:
Explanation:
The above article explains the purpose of the continue statement; the three examples provided clearly depict the usage in a real-time scenario. For, while and do-while are considered examples, and the usage of the continue statement is explained on their basis. Just like continue, there are 2 more statements called break and return, which has their own purpose and applications in java enterprise applications.
The above is the detailed content of Continue Statement in Java. For more information, please follow other related articles on the PHP Chinese website!