In this tutorial , we will learn about java continue statement.Continue Statement in java is placed inside a loop to skip the current iteration and proceed to next.We can use continue statement inside any type of loops such as
for , while , do-while loop.When the continue statement is encountered, the program jumps to the next iteration of the loop.
key points of continue statement
- The ContinueStatement is used to skip the current iteration and immediately proceed to the next iteration of the loop.
- It can be used in for , while and do-while loops.
- It is typically used within a conditional statement to skip specific iterations based on certain criteria.
- When continue is encountered, the loop’s current iteration ends, and control moves to the next iteration,checking the loop condition again.
- Using continue can simplify code by avoiding deeply nested conditional structures, thus enhancing readability and potentially improving performance by passing unnecessary code execution.
Understanding the ‘Continue’ Statement in Java
Continue Statement in real life
Continue statement is used in real life also , if you want to display the details of 10 students with roll no.s say 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 and you want to display details only of students with even roll no.s,you can use continue statement.In the given Let’s learn with simple example.
public class ContinueStudnet{ public static void main(String[] args) { for(int i = 1; i<=10; i++) { if(i%2!=0) { continue; } System.out.println(i); } } }
Output:
2,4,6,8,10 In the above example, the 'if' statement checks if the current number is odd by using % (modulo operator) . If the remainder is not equal to 0,it means 'i' is odd.If 'i' is odd ,the continue statement is executed.This is the cause of program to skip the current iteration.