|
Continue statements are very useful in some situations where we want to check a some condition and if the condition is true then only the block of code sould be executed otherwise it should skip the code and force to continue with the loop without processing the remainder of the code.
public class ContinueExample {
public static void main(String[] args) {
for(int i = 0; i < 5; i++) {
if(i <= 2) {
continue;
}
// this line will be executed only when
// the value of i is greater than 2. If it
// is not then it will skip this code.
System.out.println(i + " is greater than 2");
}
}
}
Output
3 is greater than 2
4 is greater than 2
|