Java Continue Statement
Syntax:pts in Java
1. jump-statement;
2. continue;
Java Continue Statement Example
ContinueExample.java
1. //Java Program to demonstrate the use of continue statement
2. //inside the for loop.
3. public class ContinueExample
4. {
5. public static void main(String[] args)
6. {
7. //for loop
8. for(int i=1;i<=10;i++)
9. {
10. if(i==5)
11. {
12. //using continue statement
13. continue;//it will skip the rest statement
14. }
15. System.out.println(i);
16. }
17. }
18. }
Test it Now
Output:
1
2
3
4
6
7
8
9
10
Java Continue Statement with Inner Loop
It continues inner loop only if you use the continue statement inside the inner loop.
ContinueExample2.java
1. //Java Program to illustrate the use of continue statement
2. //inside an inner loop
3. public class ContinueExample2
4. {
5. public static void main(String[] args)
6. {
7. //outer loop
8. for(int i=1;i<=3;i++)
9. {
10. //inner loop
11. for(int j=1;j<=3;j++)
12. {
13. if(i==2&&j==2){
14. //using continue statement inside inner loop
15. continue;
16. }
17. System.out.println(i+" "+j);
18. }
19. }
20. }
21. }
Output:
1 1
1 2
1 3
2 1
2 3
3 1
3 2
3 3
Java Continue Statement in while loop
ContinueWhileExample.java
1. //Java Program to demonstrate the use of continue statement
2. //inside the while loop.
3. public class ContinueWhileExample
4. {
5. public static void main(String[] args)
6. {
7. //while loop
8. int i=1;
9. while(i<=10)
10. {
11. if(i==5)
12. {
13. //using continue statement
14. i++;
15. continue;//it will skip the rest statement
16. }
17. System.out.println(i);
18. i++;
19. }
20. }
21. }
Test it Now
Output:
1
2
3
4
6
7
8
9
10
Java Continue Statement in do-while Loop
ContinueDoWhileExample.java
1. //Java Program to demonstrate the use of continue statement
2. //inside the Java do-while loop.
3. public class ContinueDoWhileExample
4. {
5. public static void main(String[] args)
6. {
7. //declaring variable
8. int i=1;
9. //do-while loop
10. do{
11. if(i==5)
12. {
13. //using continue statement
14. i++;
15. continue;//it will skip the rest statement
16. }
17. System.out.println(i);
18. i++;
19. }
20. while(i<=10);
21. }
22. }
Test it Now
Output:
1
2
3
4
6
7
8
9
10