Question about infinite loops


#1


Exercise 9: The 'do' / 'while' loop


If I run the do/while loop the lesson provided, it doesn't give me any problems and spits out "I'm gonna stop looping 'cause my condition is false!"


var loopCondition = false;

do {
	console.log("I'm gonna stop looping 'cause my condition is " + loopCondition + "!");	
} while (loopCondition);


Then I changed while (loopCondition) to while (loopCondition === false). And when I tried to run it, the browser crashed because of an infinite loop. This is where it confuses me. A few exercises before this one called "Brevity is the soul of programming," I learned that while (loopCondition) was the same thing as while (loopCondition === false) because the first line of the code declared that the variable was false. Am I missing something?

var loopCondition = false;

do {
	console.log("I'm gonna stop looping 'cause my condition is " + loopCondition + "!");	
} while (loopCondition === false);

#2

infinity loop:

var bool = true;
while(bool){
    //Do something
}

another infinity loop:

var bool = true;
while(bool === true){
    //Do something
}

and another infinity loop:

var loopCondition = false;

do {
	console.log("I'm gonna stop looping 'cause my condition is " + loopCondition + "!");	
} while (loopCondition === false);

all this loop conditions evaluate to true without a way to stop

This:

var loopCondition = false;

do {
	console.log("I'm gonna stop looping 'cause my condition is " + loopCondition + "!");	
} while (loopCondition);

is not a infinty loop, the loop condition evaluates to false (given loopCondition is false)


#3

I'm sorry. I still don't quite understand why this do/while loop works.

var loopCondition = false;

do {
	console.log("I'm gonna stop looping 'cause my condition is " + loopCondition + "!");	
} while (loopCondition);

More specifically, I don't understand the condition of the loop. If var loopCondition is false and the loop condition (while (loopCondition)) is false, wouldn't the loop go on forever since false equals false? For a second I thought the loop condition meant while (loopCondition === true). But after reading the last part of your comments, I guess it's not.


#4

no, because you don't compare if loopCondition equals false, if you did:

while (loopCondition === false);

you have an infinity loop (the loop condition evaluate to true)

in the end, it matters if the loop condition evaluates to true or false. This doesn't have to involve comparison, while (loopCondition); will simply evaluate to false because loopCondition is false


#5

So while (loopCondition); is just saying that the condition evaluates to false and stops the loop because var loopCondition = false;?


#6

yes

if loop conditions evaluate to false, they stop.

we can also do:

while(false);

maybe this will clarify something, this loop condition evaluate to false because, well, false is false (there is a surprise :stuck_out_tongue: )

Same logic applies when you have a variable containing false Boolean value.


#7

Ohhh okay, now I understand. Thank you for being patient with me and always helping me out. :smile:


#8

Hi @icemanyi, I tried to come up with my own test to illustrate your question. But, this time using a if and if/else statement so that we can see the logic behind it.

@stetim94, I don't know about involving comparison or not, but I've learn previously in IF exercises that:

When var A = true;

You can write if (A === true) or if (A), since both evaluate to true. Therefore, I would assume writing if (A === true) to compare whether A is true is equivalent to writing if (A) (which also, to me, a comparison itself) because the if syntax is:

if (checking condition evaluates to true) { 
      //run code 
}

What confuses for @icemanyi, is when var A = false;

Based on the syntax above, when:

if (condition evaluates to not true) { 
      //the code will not run 
}

means that when we write:

if (A) {  //<=== this evaluates to false, since A is "false" but the IF is checking for "true"
      //the code will not run 
}

For a better picture, I use if/else statements, using var test = false; to draw examples.
See if you can predict which console.log will print, @icemanyi.

var test = false;
if (test === false) {
    console.log("This code runs because the condition is true");
} else {
    console.log("This code runs because the condition is not true");
}

Print: This code runs because the condition is true, you're comparing if var test is false (using ===), since test is false, thus, the condition evaluates to true.

var test = false;
if (test === true) {
    console.log("This code runs because the condition is true");
} else {
    console.log("This code runs because the condition is not true");
}

Print: This code runs because the condition is not true, you're comparing if var test is true (using ===), but you have assigned var test = false, since test is not true, thus the condition evaluates to false,.

var test = false;
if (test) {
    console.log("This code runs because the condition is true");
} else {
    console.log("This code runs because the condition is not true");
}

Print: This code runs because the condition is not true, even there is no comparison, but the IF syntax check if test is true, since you have assigned var test = false, thus the condition evaluates to false.


So back to your do/while loop:

var loopCondition = false;

do {
   // run codes
} while ( loopCondition );

In this case, you want the while condition evaluates to false, so that you can break the loop cycle.
If the while condition evaluates to "true", refer above what @stetim94 explained.

That will be the cause of infinite loop.

You would want:

do {
   // run codes
} while ( a condition evaluates to false );

So you can write either while (loopCondition); or while (loopCondition === true) or anything to make the evaluation turn to false such as while (loopCondition === 123) [remember to use (===) comparision, not (=) assign] to break the loop.

However, choosing while (loopCondition); is the better solution.

Sorry for my long post, try my best to offer my opinion. Hope it helps for your situation. :slight_smile:


#9

I don't mind the long reply and it really does help, especially the quizzes you wrote. Thank You!:grin:


#10

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