1

Here is small snippet of a program I'm currently working on where the infinite loop is occurring. I have set all the variables to the values they are at in the program when it encounters this loop. The goal of this loop is to get it to either break (which it won't with these values of course) or to go through the entire loop and then move on, which I believe it should based on the conditions set in the for loop, but instead it becomes an infinite loop. I am lost and wondering if someone could help me. Why is this an infinite loop and how can I fix it? Thanks.

The first code snippet is the minimal code to show the infinite loop. The place where I have put the System.out.println("Infinite"); is where the infinite loop is occurring. It never reaches the if statement in the for loop. I have tried debugging to no avail.

int e = 1;
int f = 0;
int q = 2;
int posInt = 10;
int[] Prime = new int[posInt];
Prime[0] = 2;
Prime[1] = 3;
Prime[2] = 5;
int stay = 0;

while(stay == 0){
    while(e <= posInt){
        if((q + 1) == Prime[f]){
            stay++;
            System.out.println("BREAKING");
            break;
        }
        e++;
        f++;
    }
    q++;
}

Here is the entire program so far in case it helps. The goal is to test whether or not a number entered by the user is a prime number by using the Sieve of Eratosthenes. I realize there may be other errors in the program and it may not be right, but I'm still working on it. Currently I cannot progress though as I am stuck in this infinite loop.

    int posInt;
    Scanner reader = new Scanner(System.in);

    System.out.print("Please enter in any positive integer greater than one to see if it is a prime number: ");
    posInt = reader.nextInt();
    System.out.println();

    int n = posInt;
    int q = 2;
    int a = 3;
    int b = 1;
    int[] list = new int[n];
    list[0] = 2;

    while(a <= n){
        list[b] = a;
        a++;
        b++;
    }


    int c = 2;
    int d = 0;
    int P = 0;
    int nP = 0;
    int[] notPrime = new int[n];
    int[] Prime = new int[n];


    while(c <= n){
        if(list[d] == q){
            Prime[P] = list[d];
            P++;
        }
        else if(list[d] % q == 0){
            notPrime[nP] = list[d];
            nP++;
        }
        else{
            Prime[P] = list[d];
            P++;
        }
        c++;
        d++;


        System.out.println("Primes numbers: ");
        int y = 1;
        int z = 0;
        while(y <= P){
            System.out.println(Prime[z]);
            y++;
            z++;
        }

        System.out.println("Numbers that are not prime: ");
        y = 1;
        z = 0;
        while(y <= nP){
            System.out.println(notPrime[z]);
            y++;
            z++;
        }


        int stay = 0;
        int e = 1;
        int f = 0;
        while(stay == 0){
            while(e <= posInt){
                if(!((q + 1) == notPrime[f])){
                    stay++;
                    break;
                }
                e++;
                f++;
            }
            q++;
        }
    }

    int g = 2;
    int h = 0;
    boolean prime = false;
    while(g <= n){
        if(posInt == Prime[h]){
            prime = true;
            break;
        }
        g++;
        h++;
    }
    if(prime = true){
        System.out.println("The number " + n + " is prime.");
    }
    else if(prime = false){
        System.out.println("The number " + n + " is not prime.");
    }


    reader.close();
  • 1
    Have you tried debugging the program to see where it is looping? – clever_trevor Nov 15 '14 at 21:34
  • 1
    Please post the **minimal** code snippet that show an infinite loop –  Nov 15 '14 at 21:34
  • Please tell us exactly where your program is looping. You can find that out by either using a debugger and stepping through your code or by putting trace statements in your code (using System.out.println for instance) so that you see where the loop is occurring. – markus Nov 15 '14 at 21:39
  • The first code snippet is the minimal code to show the infinite loop. The place where I have put the System.out.println("Infinite"); is where the infinite loop is occurring. It never reaches the if statement in the for loop. I have tried debugging to no avail. – Hunter Corry Nov 15 '14 at 21:43

2 Answers2

2

The infinite loop isn't in the first snippet, it's in the surrounding while - if the break never happens, stay will also be always 0, and unless one of the other operations throws an exception you'll be stuck there forever...

    int stay = 0;
    while(stay == 0){
        int e;
        int f = 0;
        for(e = 1; e <= posInt; e++){
            if((q + 1) == Prime[f]){
                stay++;
                break;
            }
            f++;
            System.out.println("Infinite");
        }
        q++;
    }
CupawnTae
  • 14,192
  • 3
  • 29
  • 60
  • But wouldn't because of the for loop conditions, the for loop would eventually end, the variable q would increment, and then the for loop would run again? That's the goal, but the for loop never exits at all. – Hunter Corry Nov 15 '14 at 21:47
  • 1
    the `for` loop *is* ending, but then it's started all over again because of the surrounding `while`. I haven't read through all your code to see *why*, but I assume `q+1` never equals `Prime[f]` - you could try printing them out. Either way, the `for` loop *does* exit – CupawnTae Nov 15 '14 at 21:51
  • Okay that makes a lot more sense. I have altered the code from a for loop to a while loop, moved the stay, e, and f variables outside of the outer while loop, but it still ends in an infinite loop. – Hunter Corry Nov 15 '14 at 21:55
  • I edited my original post on the minimal snippet of code if that helps at all. – Hunter Corry Nov 15 '14 at 22:01
  • well now you're not incrementing `e`! :) – CupawnTae Nov 15 '14 at 22:02
  • Haha woops, all better. :) – Hunter Corry Nov 15 '14 at 22:04
  • are you still getting an infinite loop? Now I would expect an array index out of bounds – CupawnTae Nov 15 '14 at 22:05
  • Okay, now I've found out that that portion of the code, those two loops, are running flawlessly on their own. Now I've ran in to the problem q + 1 will never equal Prime[f] because of its placement in the entire code. Where should it be moved to in order to fix this? – Hunter Corry Nov 15 '14 at 22:06
  • Sorry, I don't have time to look at the entire algorithm. Hopefully you've at least got past the initial infinite loop question and can progress. It might be worth posting a new question based on your current position. – CupawnTae Nov 15 '14 at 22:15
0

You need a way to stop your while loop. So declare int f outside the loop and put it in the while condition:

int stay = 0;
int f = 0;
while (f < posInt && stay == 0) {
    int e;
    for (e = 1; e <= posInt; e++) {
        if ((q + 1) == Prime[f]) {
            stay++;
            break;
        }
        f++;
        System.out.println("Infinite");
    }
    q++;
}

And you'll have to look at the logic in the rest of your code (as stated by yourself)

NerosE
  • 362
  • 3
  • 5