-3

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);
            }
    }
}
Tom Zych
  • 13,329
  • 9
  • 36
  • 53
  • 3
    [You need to understand the basic difference between a break and a continue in that case](http://stackoverflow.com/q/462373/2024761). – Rahul Jun 16 '14 at 11:16

3 Answers3

1

Here is the situation (check the comments for descriptions of what will happen):

outer:  for (int i = 2; i<=n;i++)
    {
        for(int j = 2; j<i;j++)
        {
            if(i%j==0)
            {   
                continue outer; // skip the rest of outers loop and go to its start
            }
        }
        System.out.println("Prime Number " + i); //this will not get run 
                                                 //when the continue statement is hit
    }

With break:

outer:  for (int i = 2; i<=n;i++)
    {
        for(int j = 2; j<i;j++)
        {
            if(i%j==0)
            {   
                break; // stop running the inner loop
            }
        }
        System.out.println("Prime Number " + i); //this still gets run regardless of break
    }
RossBille
  • 1,448
  • 1
  • 17
  • 26
0

It's not 100% clear from the question, when you say replace continue statement with break does that mean your line went from:

if(i%j==0)
{   
    continue outer;
}

To:

if(i%j==0)
{   
    break outer;
}

because if so you have instructed the program to break the outer loop and thus execution of both loops will stop

RossBille
  • 1,448
  • 1
  • 17
  • 26
  • No i mean if(i%j==0) { break;} – user3247825 Jun 16 '14 at 11:18
  • @user3247825 , ok in that case you have instructed the program to stop executing the inner loop, thus running `System.out.println("Prime Number " + i);`before running the next iteration of the `outer` loop. With `continue outer;` you have told the program to jump to the top of the `outer loop` which will skip the execution of line `System.out.println("Prime Number " + i);` – RossBille Jun 16 '14 at 11:23
  • Thanks for the help Sir. It really helps me in understanding the behavior of the code. – user3247825 Jun 16 '14 at 11:29
0

continue start over;

for(int i=0;i<5;i++){ 
continue; // restart iteration with next value in index i=1;
}

break execute code after loop

for(int i=0;i<5;i++)) { 
break; // execute from  from next line
}

// next line

Sanjay Rabari
  • 2,091
  • 1
  • 17
  • 32