Intro to 'While' Loops 11/11


#1

I wrote out this code and it works all the way up until the do/while part...then it goes infinite and I'm not really sure why/what the problem is.. Thanks!

var firstLoop = [1,2,3,4,5];
for(var i=0;i<1;i++)
{
    confirm("Do you want to continue?");
    console.log("great.")
};
var secondLoop = true
while(secondLoop)
{
    confirm("more...?");
    secondLoop = false;
};
var thirdLoop = true
do
{
    console.log("Yay! You've reached the end!");
};
while(thirdLoop);
thirdLoop=false;

#2

hi ,your code its quite nice ,,just a little bit misstakes...

 var firstLoop = [1,2,3,4,5];
    for(var i=0;i<1;i++)
    {
        confirm("Do you want to continue?");
        console.log("great.")
    }
    var secondLoop = true
    while(secondLoop)
    {
        confirm("more...?");
        secondLoop = false;
    }
    var thirdLoop = false;
    do
    {
        console.log("Yay! You've reached the end!");
    }
    while(thirdLoop){

        thirdLoop=false;
    }

remember that dont use ; after }


#3

help me in the lesson

When to 'while' and when to 'for'


#4

Have a look at this FAQ:


#5

Yes the problem is the ; after the } of do. So I deleted the bug tag from this one. But @jerry_89 and @sarah_cohen be careful with your do while loops:

 do
    {
        console.log("Yay! You've reached the end!");
    }
    while(thirdLoop);

this is what the loop is if you attach something like this:

{

        thirdLoop=false;
    }

it will in no way correspond to the execution of the loop but will just be executed after the loop is already finished. So if you define a stopping condition here you might end up in an infinite loop.


#6

"for " usually when you know when to stop the loop .. for example ..

there many names in a SQL table .i want pint them all out 1by 1 ,,

so i will first put them in an array ,,and then use for print them all out ..

in fact “while” is not that often to use ....

while is when the condition quals true ,it will keep running .

so if you don't make it becomes to false .its going to crash your browser ...

my english sucks . hope you understood..


#7

I fixed the problems (I THINK) but it keeps crashing my browser when I run it...Is there nothing else wrong with it?

var firstLoop = [1,2,3,4,5];
for(var i=0;i<1;i++)
{
    confirm("Do you want to continue?");
    console.log("great.")
}
var secondLoop = true
while(secondLoop)
{
    confirm("more...?");
    secondLoop = false;
}
var thirdLoop = true
do
{
    console.log("Yay! You've reached the end!");
}
while(thirdLoop)
{
    thirdLoop=false;
}

#8

The problem is that the do/while loop doesn't work like this:
Have a look at the links here for a longer explanation:


#9

A post was merged into an existing topic: Hi dear you help me in this problem