I was writing code to generate the prime numbers using nested loops in Java and with the help of a friend I made a program which successfully generates prime numbers.
In this code my friend suggested that I use continue label
statement to transfer the control to outer loop and it works. However, when I replace the continue
statement with break
, it doesn't give me the answer it should. Can anyone please explain what is the mistake or the reason behind this behavior?
The code is:
import java.io.*;
class Prime
{
public static void main(String x[]) throws Exception
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
outer: for (int i = 2; i<=n;i++)
{
for(int j = 2; j<i;j++)
{
if(i%j==0)
{
continue outer;
}
}
System.out.println("Prime Number " + i);
}
}
}