Just need a little help understand how this is working


#1



https://www.codecademy.com/courses/javascript-beginner-en-ASGIv/0/3?curriculum_id=506324b3a7dffd00020bf661

I am not having any issues with my code or anything, but I would to get some clarity on how it is working. before the while loop starts "understand" is = to true. However, what I have a problem with is when we change the value of "understand". when we set the value of "understand" to "false", why does this stop the loop? I know the loops stops when the condition is false, as in 2 === 1 or something like that. In my mind the condition is still true, because it is just saying understand is false and I am thinking "yes that is correct, it is false" so, it should keep running. Does this mean we all we have to do to stop a while loop is set the condition value to "false" and it will stop, we don't have to have a condition that is false like in my example of 2 ===1?


understand = true;

while(understand){
    console.log("I'm learning while loops!");
    //Change the value of 'understand' here!
    understand = false
}


#2

At first you assigned understand = true;
then your condition is
while(understand) means while(understand==true)
it will console "I'm learning while loops!"
then you assigned understand = false
now understand==false
and your condition is while(understand==true)
so should it keep running ?
i hope now you have understand :slight_smile:


#3

so as long as you change the initial value of the loop condition it will stop the loop?


#4

yes it should be stop :slight_smile:


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.