While/do Review


Good night, I would like to know why the condition do it only goes with the var FALSE .
I changed for TRUE and it didn't go.
Thank you.

var win = false;
do {
console.log("esse é o último!");
while (win);

var win = true;
do {
console.log("esse é o último!");
while (win);


Do-while runs once regardless of the condition.

If your condition is always true then you have an infinite loop which you might want to avoid.


@tainaml Because Codecademy has a built in anti-infinite loop code thingy (very technical term :smile:). So, when win is true, it exits without doing anything, because it just kept running... and running... and running. But do/while loops run once BEFORE checking if win is true; so, when win is false, it runs once, and then exits.


Does it? I thought infinite loops would just lock up your browser since JS exercises run locally.
Most other courses run on a server, and yeah, those time out after a while.

I would expect what happens when win=true is that the browser would get very busy printing the same message over and over again to the console window.

I doubt it quits before doing anything! That would suggest being able to tell in advance that something is an infinite loop. That can be very hard to tell sometimes.


@ionatan Maybe not, I'm not quite sure anymore, but several of the other sites that I use do.