user5433306
user5433306

Reputation: 13

My do while loop gets stuck in an infinite loop

I'm learning JavaScript. I just learned do while loop and tried to do some example.

This is my code:

for (i=0; i<5; i++) {
    console.log ("for loops " + i);
}
do {
    console.log ("do once");
} while (i<8) {
    console.log ("while loops " + i);
    i++;
}

And I expected the result below.

for loops 0
for loops 1
for loops 2
for loops 3
for loops 4
do once
while loops 5
while loops 6
while loops 7

But, unfortunately, my browser went into an infinite loop. Why is this loop infinite?

Upvotes: 1

Views: 980

Answers (5)

kannan
kannan

Reputation: 711

You need to make sure that increment inside your loop. See this example.

for (i=0; i<5; i++) {
console.log ("for loops " + i);
}
console.log ("do once");
do {
console.log ("while loops " + i);
i++;
} while (i<8);

You are trying to increment in some code that will never execute. You appear to have confused a do...while() loop with a while() loop.

A do...while() will execute once before it checks the while clause and a while() will check the clause before it loops. They are slightly different.

Upvotes: 0

samir benzenine
samir benzenine

Reputation: 478

"The do/while loop is a variant of the while loop. This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true."

i=5 when the code reaches the Do/While loop. So the code will repeat "console.log ("do once");" until i is greater than 8. As a result, it goes into an infinite loop.

Upvotes: 1

mld
mld

Reputation: 36

In JavaScript, a do-while loop has the following syntax:

do{
  commands();
} while (logical statement);

so you wrote:

do {
  console.log ("do once");
  // missing increment
} while (i<8);

{ // these { } are not part of any loop, but just a scope block
  console.log ("while loops " + i);
  i++;
}

the browser gets stuck in the infinite loop since variable i is not being incremented inside the while loop. you should try the following:

for (i=0; i<5; i++) {
  console.log ("for loops " + i);
}

console.log ("do once");
do {
  console.log ("while loops " + i);
  i++;
} while (i<8);

hope that helps

Upvotes: 0

Darren
Darren

Reputation: 70718

Effectively you have wrote:

do {
    console.log ("do once");
} while (i<8) 

The variable i is never being incremented and has caused the infinite loop (Since the value is always smaller than 8).

To fix this you could increment the variable within the loop.

do {
    console.log ("do once");
    i++;
} while (i<8);

http://jsfiddle.net/uby3nfoy/1/

Your confusion maybe between the two different type of loops. do while and while.

Upvotes: 1

MC ND
MC ND

Reputation: 70923

for (i=0; i<5; i++) {
    console.log ("for loops " + i);
}

do {
    console.log ("do once");
} while (i<8) 

{
    console.log ("while loops " + i);
    i++;
}

Maybe this way you can see the reason

The "problem" is that the interpreter will see the while as part of the do

Upvotes: 1

Related Questions