简体   繁体   English

if语句中while循环的功能是什么? JavaScript的

[英]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. 下面是一个函数,它返回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语句中的while循环。

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? 如果之后没有代码,while循环的目的是什么?
  2. What is happening when the while loop evaluates true? 当while循环评估为true时发生了什么?
  3. What is happening when the while loop evaluates false? while循环计算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. 注意while语句中的x = x+2 It adds 2 to the value of x repeatedly until the while clause evaluates true. 它会反复将x的值加2,直到while子句的计算结果为true。

So a while loop without a body blocks execution until it's clause becomes true. 所以while循环没有body阻塞执行,直到它的子句变为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. 除此之外,在条件中将两个加到x右边,所以不需要一个体。 The loop will execute until x < root or num % x fails to be true. 循环将执行,直到x < rootnum % x无效为止。 The body just doesn't have any instructions in it. 身体里面没有任何指示。

Its very similar to what happens when you execute a for loop 它与执行for循环时发生的情况非常相似

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 . 看看for循环体中没有指令,但循环仍然会向i添加一个,直到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 . 实际上,此代码表示如果x不能被3整除,则将2加到x并继续,如果结果值小于root The loop terminates as soon as one of these conditions is no longer true, but, meanwhile, x has been updated along the way. 一旦这些条件中的一个不再成立,循环就会终止,但同时, x已经沿途更新。

Note that x = x + 2 evaluates to the assignment's left operand. 请注意, x = x + 2计算赋值的左操作数。

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是一个赋值,你可以看到它不是=====运算符而是a =

(x = x + 2) < root means increment x by 2 , and then compare it to root . (x = x + 2) < root表示将x递增2 ,然后将其与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. 通常不建议在whileif语句的条件部分中使用赋值,因为它会降低代码的可读性。

The while statement there does change the value of the x variable. while语句确实改变了x变量的值。 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. while条件/循环中有一个赋值。 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). 它在循环运行时进行评估,因此每个循环都会更新“x”的值,直到不再<root(除了&和:&& 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. 看起来你的挂机事实是,在while语句中没有正文。 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. 基本上while循环会反复检查条件。 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'. 在这种情况下,'x'将增加2,直到达到或超过'root'的值。

credit to @Teemu for "reaches or" 归功于@Teemu的“达到或”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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