Continue Statement in java

Continue Statement in java

continue statement is used in decision making such as if-else etc .continue statement is completely different from the break statement because continue statement doesn’t exit the loop if the condition is false, continue statement will jump controller again to the starting of the loop.

syntax:

continue;

flowchart:

Flowchart of Continue Statement in java

Working of break statement:

Working of break statement in java

Example

import java.util.*;
class X2
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
int i=0,x=0,sum=0;
for (i=1;i<=5;i++)
{
System.out.printf("enter %dst number ",i);
x=s.nextInt();
if(x<0 || x==0)
{
continue;
}
sum += x;
}
System.out.printf("the total sum is %d",sum);
s.close();
}
}

output

enter 1st number 12
enter 2st number 0
enter 3st number -20
enter 4st number 2
enter 5st number 2
the total sum is 16

when program encounters the continue key word ,the program skips the itreartion,in the abov example when the in puts are 0,-20 these are skipped and not inclued in the sum variable.the program output will be 12+2+2=16 and 0,-20 are ignored with the continue statement.

Last updated on by vishal devxo