In this post, we will see what is continue statement in java.
The continue statement in Java uses in a loop control structure. If you want to skip some code and need to jump to the next iteration of the loop immediately. Then we should use it. It can be used in for loop or while loop.
continue;
Youtube video available in the Hindi language
continue statement in java inside for loop
If a continue statement encounters inside a loop, the control transfers to the next iteration and the rest of the code don’t execute for the iteration. It uses along with the if statement in loops. If the condition returns true, then the continue statement transfers the control of the loop immediately to the next iteration. Unlike the break statement, It doesn’t break the execution of the loop. It just skips one iteration of loops.
The continues statement is useful whenever use want to skip the iteration based on conditions.
public class ContinueExampleForExample { public static void main(String args[]) { for (int i = 1; i <= 5; i++) { if (i == 2) { continue; } System.out.println("Value of i ="+ i); } } }
Output:
Value of i =1
Value of i =3
Value of i =4
Value of i =5
In the above example, the “value of i = 2” is missing in the output, because when the value of the variable i is 2, the program encountered a continue statement, which makes it jump for next iteration, skipping the statements for the current iteration.
1. Quiz, Read the below code and do answer.
Click on anyone to know the answer.