A fellow of infinite loops


#1

I don't understand this part of A fellow of infinite loops while loop,

The variable understand isn't being compared to any value inside the while loop parenthesis. So I want to know how the condition is satisfied. Can I write it has "understand === true"? It is also printing false in the console for some reason.

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

#2

you don't always have to compare, in the end it matters if the condition is true or false.

so a simple:

if (true){
    console.log("see")
}

is perfectly valid

you update a variable after the last console.log, this will log the variable value (console behavior), you could simply swap the console.log and updating understand around to get rid of the problem


#3

Imagine you're in class and the teacher's giving a lesson. You understand whatever he's explaining, until you don't anymore and need to let him know.

Well, here you understand (understand = true). While you're understanding stuff being explained to you, you keep getting more information your way...

... until you don't understand anymore (understand = false).

While being taught all this stuff, you don't need to keep saying "Yeah, I understand" (you let the teacher speak, right), you just stop the explanation when you don't understand anymore. You break it, in order to avoid an infinite loop.


#4

So the condition is true because the variable understand has been declared as true in the first line of the code?


#5

Thanks, but I know how the while loop works, I was just confused about the condition being satisfied. :slightly_smiling_face:


#6

@stetim94 "False" gets printed in the console because it's default behaviour of the console? How do I make sure that false isn't printed in the console, by putting the console.log in the last line of the loop?


#7

yes, in the end it boils down to if the condition is true or false, it doesn't matter if this achieved through comparison, it can also simply be a variable being evaluated, given understand is true, the condition will be true

yes, given now no variable is update after the last console.log statement


#8

Thank you for explaining it mate, understood it perfectly, :slightly_smiling_face:


#9

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