简体   繁体   中英

What is the functionality of the while loop inside the if statement? JavaScript

Below is a function that returns the prime factors of a given number in JavaScript. I did not write the function but have been studying it to extend my programming knowledge.

My questions are about the while loop that is inside the following if statement.

if(num % x){
    x = 3; 
    while((num % x) && ((x = x+2) < root));
}

Questions

  1. What is the purpose of a while loop if there is no code after it?
  2. What is happening when the while loop evaluates true?
  3. What is happening when the while loop evaluates false?

Here is the function in it's entirety.

function getPrimeFactors(num){
    num = Math.floor(num);
    var root = 0;
    var factors = [];
    var doLoop = 1 < num;
    var x = 0;

    while(doLoop){
        root = Math.sqrt(num);
        x = 2;

        if(num % x){
            x = 3;
            while((num % x) && ((x = x+2) < root));
        }

        if(x > root){
            x = num;
        }else{
            x = x;
        }

        factors.push(x);

        doLoop = (x != num);

        num = num/x;
    }

    return factors;
}

Thanks for the help!!!

Note the x = x+2 in the while statement. It adds 2 to the value of x repeatedly until the while clause evaluates true.

So a while loop without a body blocks execution until it's clause becomes true. This is only a good idea if you you are mutating a variable in the clause as part of the condition. Otherwise you may end up in an infinite loop.

It is really doing something like this:

if(num % x){

    x = 3;

    while(num % x){

        x = x + 2;

        if(x < root)
            continue;
        else
            break;
    }

}

Except, two is added to x right in the conditional, so there is no need for a body. The loop will execute until x < root or num % x fails to be true. The body just doesn't have any instructions in it.

Its very similar to what happens when you execute a for loop

for(int i=0; i < n; i++)
    ;

See there are no instructions in the for-loop body, but the loop will still add one to i until i >= n .

This code says, in effect, that if x is not evenly divisible by 3 , then add 2 to x and continue if the resulting value is less than root . The loop terminates as soon as one of these conditions is no longer true, but, meanwhile, x has been updated along the way.

Note that x = x + 2 evaluates to the assignment's left operand.

There is actually one thing happening in the loop:

                       V
while((num % x) && ((x = x+2) < root));

x = x + 2 is an assignment, you can see it is not a == or === operator but a = .

(x = x + 2) < root means increment x by 2 , and then compare it to root .

Using an assignment in the condition part of a while or if statement is generally not recommended, because it makes the code less readable.

The while statement there does change the value of the x variable. It could be rewritten as something like the following:

if (num % x) {
    x = 3;
    if (num % x){
        do {
            x = x + 2;
        } while (num % x && x < root);
    }
}

x的值递增(乘以2),直到它等于或大于root的值

There's an assignment in the while condition/loop. It's evaluated while the loop is running so the value of "x" gets updated every cycle until no longer < root ( besides/and: && num % x < root).

Very clever but I hope this is not an introduction text/book/post to programming.

It seems like your hang-up you're having is with the fact that there's no body in the while statement. Understand that a loop body is optional and that it it behaves in the same way as a loop that does have a body.

Basically the while loop will repeatedly check the condition over and over. Yes, this can result in an infinite loop if coded incorrectly. In this case, 'x' will be incremented by 2 until it reaches or exceeds the value of 'root'.

credit to @Teemu for "reaches or"

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