When you are working with loops where loop body is repeatedly executed, you may have a scenario where you want to skip the execution of statements inside the loop or you may want to terminate the loop altogether. To handle these two scenarios there are two control statements in Java- continue statement and break statement. In this tutorial you’ll learn about Java continue statement along with usage examples.
When to use continue statement in Java
During the repeated execution of the loop if you don’t want to execute statements with in the loop body for some particular condition you can force the next iteration of the loop using continue statement.
If there is a continue statement in a loop statements after the continue are not executed and control jumps to the beginning of the loop.
If continue statement is there in while loop or do-while loop control transfers to the condition of the loop.
In case of for loop, continue statement causes the control to transfer to the iteration part first and then to condition part.
Continue statement Java examples
1- Using continue statement with while loop. In the example you want the user to enter an even number when such a number is entered then only control comes out of the loop otherwise loop continues.
public class ContinueJava { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int number; while(true){ System.out.print("Enter a number: "); number = scanner.nextInt(); // checking entered number even or not if(number%2 != 0) { System.out.println("Please enter even number..."); continue; }else { break; } } scanner.close(); System.out.print("Entered number is- " + number); } }
Output
Enter a number: 5 Please enter even number... Enter a number: 7 Please enter even number... Enter a number: 8 Entered number is- 8
2- Using continue statement in for loop.
public class ContinueJava { public static void main(String[] args) { for(int i = 0; i < 5; i++) { if (i == 1) continue; System.out.println(i); } } }
Output
0 2 3 4
As you can see when value of i is 1 continue statement is encountered so the statement after the continue statement is not executed and the control transfers to the beginning of the loop for next iteration.
3- Using continue statement in do-while loop. In the example odd numbers between 1..10 are displayed using do-while loop.
public class ContinueJava { public static void main(String[] args) { int i = 1; do { // check if even number if(i%2 == 0) continue; System.out.println(i); }while(++i < 10); } }
Output
1 3 5 7 9
Labelled continue statement in Java
Just like labeled break statement there is also a labeled continue statement to let you decide which loop to continue.
Labelled continue statement Java example
In the example a pattern (triangle) is displayed using labeled continue statement.
public class ContinueJava { public static void main(String[] args) { outer: for (int i = 0; i < 6; i++) { for(int j = 0; j < 6; j++) { if(j > i) { System.out.println(); continue outer; } System.out.print("*"); } } } }
Output
* ** *** **** ***** ******
In the example whenever value of j is greater than i control is transferred to the outer for loop for next iteration.
That's all for this topic continue Statement in Java With Examples. If you have any doubt or any suggestions to make please drop a comment. Thanks!
>>>Return to Java Basics Tutorial Page
Related Topics
You may also like-
No comments:
Post a Comment