简体   繁体   中英

While-loop evaluation condition

Sorry if the question seems really trivial but I'm just learning how to code and this one kept me in front of the computer for about 2 hours without getting to realize why this happens.

I'll pass the code below. So, in order to keep it straightforward:

isPrime() is a function that just checks if a current number is Prime or not.

nPrime() is a function that returns the n-ism prime number, given N as the parameter.

The key point here is the main function and, more precisely, the number value in the first while-loop .

If you run this code, when it reaches the last prime number by which number is divisible, it'll enter an infinite loop. This can be easily solved if you just change the first while condition from while(number > 0) to while(number > 1) .

That's the weird thing I can't come to realize: If the inner second while-loop won't exit as long as number % nPrime(index) != 0 and the last instruction of the outter first while-loop is number /= nPrime(index); , how come the program enters an infinite loop?

That last instruction set number's value to 0, so the first while-loop condition should return false and exit the loop.

What am I missing?

Thank you all for your time and patience.

PS: I got downvoted and I don't know why, so I'll make an clarification:

I've done the research. As far as I know, every source seems to agree on the same point:

the > condition returns true if and only if left operand is greater than right operand .

Which takes me to the previously written question: if number is equal to 0, how's the while-loop not evaluating the number > 0 as false and exiting from the iteration?

#include <iostream>
using namespace std;

bool isPrime(int);
int nPrime(int);


int main() {


    int number = 264;

    if (number > 0)
    {
        int index = 1;

        while(number > 0)
        {
            while (number % nPrime(index) != 0)
            {
                index++;
            }
            cout << nPrime(index) << endl;
            number /= nPrime(index);
        }

    }

    else
        cout << "Error";


    return 0;
    }


bool isPrime(int n)
{
    bool isPrime = false;
    int totalDividends = 0;

        for (int i = 1; i <= n; ++i)
        {
            if (n % i == 0)
                totalDividends++;
        }


    if(totalDividends == 2)
         isPrime = true;

    return isPrime;
}

int nPrime(int n)
{
    int result = 0;

    for (int i = 0; i < n; ++i)
    {
        do
        {
            result++;
        } while (!isPrime(result));
    }

    return result;
}   

What am I missing?

That last instruction set number's value to 0

No it doesn't, it never gets that far. When number equals one then number % nPrime(index) != 0 is always true, so the inner while loop never exits.

Your understanding of while loops is perfect, it's your understanding of what your own code does that is in error. This is normal for bugs like this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM