Continue statement in java with example

Continue statement tell the compiler to skip the following statament and continue with the next iteration

Syntax:-

continue;

Continue statement in for loop

Here continue causes the control to go to increment section(3rd section in for loop) of the loop than condition statement(2nd section in for loop).

Example:-

Java program for display odd number using continue statement

import java.util.Scanner;

class group{
public static void main(String angt[]){
Scanner data = new Scanner(System.in);
int num,i,j;
System.out.println("Enter a number");
num=data.nextInt();
for(i=1;i<=num;i++)
{
j=i%2;
if(j == 0)
continue;
else
System.out.println(i);
}
}
}

Continue statement in while and do while loop

Here continue causes the control to go to condition statement of loop.

Example:-

Java program display even number using continue statement

import java.util.Scanner;

class group{
public static void main(String angt[]){
Scanner data = new Scanner(System.in);
int num,i;
System.out.println("Enter a number");
num=data.nextInt();
i=0;
while(i<=num)
{
 i++;
if((i%2)==0)
continue;
else
System.out.println(i);
}
}
}

Previous Code:-
Break statement in java

List Code:-
Java codes

Next Code:-

Goto statement(labeled break)


Java Programs:-
List of Java Programs

One Comment

  1. Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as
    appvn

    ReplyDelete

Add your comments here

Back to Top