It makes no sense how while loops operate?!


#1


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


I made the code correctly, cus I found it in a thread. But can somebody explain what happens here?
Does the while loop check if understand equals true and if so it prints out false?
To me it makes no sense, hope someone can help


var understand = true;

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


#2

var understand = true; //here you took a variable named understand and set it to true

now

while( understand) // here it means while( understand==true)

The while loop loops as long as a specified condition is true.

so here while( understand ) it checks understand equals to true or not.As it's true
now console.log("I'm learning while loops!");

then you set understand = false;   //it's very important

becuse now it'll again check understand equals to true or not.this time it's false.so it'll end now.but if you forget to set it to false the loop will never end.This will crash your browser.

hope you've understand now :slight_smile:


#3

Yes, makes a lot more sense!
It was the (understand) and (understand === true) meaning the same thing that hadnt been explained that confused me..
Thanks!


#4

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