Java continue keyword example
- Details
- Written by Nam Ha Minh
- Last Updated on 19 August 2019   |   Print Email
In Java, the continue keyword is used to stop execution of a current iteration in a for loop or a while loop, then advance to the next iteration. The statements after the continue keyword won’t be executed. The syntax is as follows:
for/while (expressions) { // statemenst 1... if (condition) { continue; } // statements 2... }
The following example uses continue keyword to skip the even value of the variable i, thus the for loop will print out all the odd numbers from 1 to 100:
for (int i = 1; i < 100; i++) { if (i % 2 == 0) { continue; } System.out.println(i); }
Similarly, the following while loop will produce a list of even numbers from 1 to 100:
int count = 1; while (count <= 100) { if (count % 2 != 0) { count++; continue; } System.out.println(count); count++; }
Related keywords: for, while. See all keywords in Java.
Related Topics:
Other Recommended Tutorials:
- 9 Rules about Constructors in Java
- 12 Rules and Examples About Inheritance in Java
- 12 Rules of Overriding in Java You Should Know
- 10 Java Core Best Practices Every Java Programmer Should Know
- Understand Interfaces in Java
- Understand how variables are passed in Java
- Understand encapsulation in Java
Comments