What's wrong? Infinite loops, that's what! Help! 2/11


#1

I'm really confused about this one. Does anyone know how to fix this? I've tried doing booleans, true and false, I've tried just 0 and 1, with and without spaces, and I'm out of ideas. Either it's wrong, or it's an infinite loop. I need help!
var understand = true;

while(what should go here???!){
console.log("I'm learning while loops!");
understand = false;
}


#2

while(understand)

thats your answer.


#3

The idea of a while loop is that it loops while the condition (the stuff in () after while) is true and stops if it is false. So the booleans true and false aren't that good here as false never starts and true never stops. The best idea is to make your condition depend on a variable e.g. you could use understand as condition as @codecoder08545 suggested. Then you would start the loop with understand being true but in the loop you would reset it to false so you stop. In the end the loop therefore ran just once. Of course you can do more complex stuff or try to emulate a for loop. You can also have a look at my post over here:
http://discuss.codecademy.com/t/faq-and-some-explanations-about-loops/1201/2?u=haxor789
which deals with all kinds of loops and also has some examples how they work and how you can interchange between them.


#4

Thanks, @haxor789. You're so helpful to me and everyone else. I doubt I'd ever understand these things without you and all the other helpful people here. (Here's a little gift to give you for helping everybody! :gift:)


#5

But I also have a question. Could you use a while loop to, say, print the same thing out on the console several times and then stop the loop? Or is it just once, forever, or not at all?


#6

Thank your, your welcome! :blush:


#7

In this case you'd probably use a for loop as you can there define a clear start and end point but sure you could do this as well with a while loop. In the second post of the link is an example how you could interchange for and while loops.