Help!why is this an infinite loop?


#1

this is what i just wrote - seemed to me failsafe and really simple to:

var count=0;
var getToDaChoppa = function (){
do {
console.log("bla!");
}
while (count=1);
};

getToDaChoppa();

as I understand it, my variable is 0, the "do" forces the function to say "bla!" once, and then it checks the while condition, which is false (because count=0), and then it should stop.

but no: my browser crashed.
any idea why?

thanks!


#2

just found something on this on w3 schools it says:
"Note: If you are using a variable with the condition, initialize it before the loop, and increment it within the loop. If you forget to increase the variable, the loop will never end. This will also crash your browser."

why would it crash my browser?


#3

var count=0;

var getToDaChoppa = function(){
do {
console.log("bla!");
}

while(count++<2){
    console.log("Something...");
}

// Write your do/while loop here!

};

getToDaChoppa();

this worked! I still wonder why not increasing the variable crashes the browser though (instead of running the function once, aborting, and that's that.) Is it because the while loop, even if the condition is false, sends the computer all the way up to do again, and it would have to print "bla!" indefinitely?

thanks!


#4

This is not a conditional expression since it is in the form of an assignment. An assignment is not a comparison, so cannot be understood by the compiler. It is looking for a Boolean which is what all conditionals yield.

while (count===1);

#5

and last question on this, i promise: the console.log printed for this:
bla!
bla!
bla!
Something...

but why? shouldn't it have printed bla! once, because i told it to, then evaluated the while, which would have been 0<2, gone on to print "something...", and then done the whole thing again 3 more times so:

bla!
Something...
bla!
Something...
bla!
Something...
?


#6

so you are saying a while statement must contain not just a value that is true but a comparison that yields true? but in the examples they used also a variable that they simply defined beforehand to mean true, like
var understand=true;

while (understand) {}


#7

A condition that yields true will permit the loop to continue. The key is in understanding the term, expression. It is not a statement and cannot be executed, only evaluated. Expressions always return a value of some kind which can then be converted to a Boolean. This is called coercion or type conversion and JavaScript does it behind the scenes all the time. It is desperate to make everything fit the scenario.

An identity requires no coercion. It is either identical or not. Ditto for value relations. Less than, greater than, etc. all yield simple Booleans. In general, comparisons are pretty straight forward expressions, It does get stranger, though.

 if (foo()) { // ... }

In the above, foo() is an expression. The yield is the return value of the function foo. It is then evauated in the conditional and converted to a Boolean.