In Java, controlling loops is key to writing good code. One way to do this is with the ‘Continue’ Statement. Now, we’ll explore what it does, how to use it, and what it means for your Java program.
‘Continue’ Statement in Java With Example
The ‘Continue’ statement is a built-in feature of java, always at your service whenever you’re working with loops. You don’t need to fetch it form any external source it’s right there in the language itself.
Imagine you’re working with a loop and you want to skip some parts of it based on a condition. That’s where ‘Continue’ comes into the picture. It’s like saying, “Hey, if this condition is met, let’s just skip this part and move on to the next iteration.”
Example of ‘Continue’ Statement
public class continue_statement{ public static void main(String args[]){ for (int i = 1; i <= 10; i++){ if (i % 2 == 0){ continue; //Skip the even number } System.out.println(i); } } }
Output: 1 3 5 7 9
Explanation:
We initialize a loop to iterate through numbers 1 to 10.
If a number is even, we skip to the next iteration using “continue”.
For odd numbers, we print them out.
This process continues until ‘i’ reaches 10, ultimately printing only odd numbers.