Why doesn't this while loop STOP


#1



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


Why doesn't this while loop STOP???


var understand = true;

while(understand=true){
	console.log("I'm learning while loops!");
	var understand=false;
}


#2

I'm having the same problem. I can't figure out what the proper syntax is. Anyone else have a solution?


#3

while(understand=true)//wrong
you should write
while(understand==true) // equal to
or
while(understand===true) // equal value and equal type
or
while(understand)

because you are checking the value :slight_smile:


#4

wrong means in this case that you assign the value true to the variable understand, overwriting the current value. And the whole name = value expression has the value you assign so as the value is in this case true you have the classic infinite loop of while(true)


#5

Simple explanation is do not give an assignment operation when you are testing for a condition. = is the assignment operator and the while loop takes a condition to test for truthfullness.


#6

var understand = true;

while(understand) {

}

thats all you need no need to put a comparasion in this case ,since understand is true! it will run the code and it will stop asap it loops once since you give it a false value when it runs with true. Think it as a robot that goes line by line reading what you code.

ex: Oh i reach while and my ( ) evaluates true ..lets go to the next line
oh i need to print this..
Oh now it changed to false..stop no more printing this.


#7

Feeling really lost on fundamental stuff suddenly.

I understand how the while loop is supposed to work, except for how to write the condition. I literally don't understand anything about this condition bit shown here. (It doesn't help that the variable is called "understand" which just seems to be rubbing it in.) The hints say nothing about it either. All the previous conditions have been easier to get, and involved numbers and iterations. This true/wrong thing is seeming to come out of nowhere.

First - if you set var understand = true, doesn't that just mean the variable is always true?
What is all this "wrong" business with slashes? I don't remember ever being asked to use wrong or slashes in this way or at all, really.

Is any of this explained anywhere? I've been working through JS in order. Did I somehow miss a lesson? The explanation in the previous step only says not to worry about the "math" stuff in the condition, so there's nothing to compare it to.