What is the endless cycle of while? Why did you enter an endless cycle?

let status = true;

while (status) {

}

console.log ("not executed here");

Mar.01,2021

while (true) is of course an endless cycle. There is no reason why


while sends a true into it, which is equivalent to being in a loop all the time. There is no end


.

Xiong du, you status are all assigned true values

No wonder the endless cycle


your code execution flow

let status = true;

while (status) {   //  true {} false  status    true

}
...

unless there is a statement in your while loop that modifies the status variable value to the false state, it must be an endless loop.


first of all, almost all the program control keywords in the programming language, including judgments, loops, and so on, are actually derived from if; in other words, if the language allows, they can all be written as if.. The form of.
second, come back to the question. Then while can be transformed into:

Label:
    If (xxx) {
        dosomething;
        Goto Label;
    }

this form. Of course, this is not javascript, because it removes goto and retains only label, when it is designed, so you just have to be able to understand what it means. So we know that if (true) is actually a "common" logical statement without judgment, so at this point, this paragraph actually becomes:

Label:
    dosomething;
    Goto Label;

has no control, and the program is constantly shuffling back and forth between performing actions and jumping to label paragraphs, that is, an endless loop.


let status = true;

while (status) {

}

you don't change the value of status in while, so it's always true, and goes all the way here, forming an endless loop

.
Menu