0

I tried to use a while loop inside a for loop, but everytime when I try do this my browser crashes. I know this happens when the loop is infinite, but I can't seem to figure out why.

for(i=0;i < 2; i++)
{
 console.log("hello");
 while(i < 2)
 {
 console.log("it's me");
 }
}
asked Mar 22, 2017 at 18:12
7
  • 4
    Because i is not changed inside the while loop, this loop will never stop. Commented Mar 22, 2017 at 18:13
  • 1
    When did you expect the while loop to stop? Did you mean if? Or what overall console output did you desire? Commented Mar 22, 2017 at 18:13
  • 1
    it is never going to stop as i will never be greater than 2 in your loop and your browser will become unresponsive. Commented Mar 22, 2017 at 18:13
  • You need to change the "while" to an "if" and then perhaps it will do what you wanted... Commented Mar 22, 2017 at 18:14
  • Thanks guys, thought I could use the i from the for loop. Commented Mar 22, 2017 at 18:16

4 Answers 4

3

You loop the while loop forever, because i keeps the value and is never changed inside.

I think you might use better an if statement to get the additional output.

var i;
for (i = 0; i < 2; i++) {
 console.log("hello");
 if (i < 2) {
 console.log("it's me");
 }
}

answered Mar 22, 2017 at 18:13
Sign up to request clarification or add additional context in comments.

Comments

1

The problem is the while loop, once i becomes 0, it will never come out of the while loop.(Since it is less than 2)

Possible solutions:

SOLUTION 1: Remove the while loop present inside the for loop

SOLUTION 2: Handle inside the while, and break after doing something

 for(i=0;i < 2; i++)
 {
 console.log("hello");
 while(i < 2)
 {
 console.log("i < 2");
 break;
 }
 }

SOLUTION 3: Change the value of i>=2 inside the while loop, so that, the loop breaks

Deepak
4521 gold badge6 silver badges15 bronze badges
answered Mar 22, 2017 at 18:46

1 Comment

Thanks great explanation, haven't thought about the while loop being stuck so the i wont ++
1

What's happening is your while loop is never ending; the value of i never changes inside the loop, so the loop continues forever.

Maybe what you were looking to do was log the message "it's me" when i, in the for loop, was < 2. In that case, you can use a simple if statement, such that your code reads something like this:

for(var i=0;i<2;i++){
 console.log("hello");
 if(i<2) console.log("it's me");
}

I'd recommend playing around with the number values and testing it out to get a better feel for how JS syntax works.

answered Mar 22, 2017 at 18:13

Comments

0
 for(i=0;i < 2; i++)
 {
 console.log("hello");
 while(i < 2)
 {
 //Once this block is entered, value of i is never changed and the condition
 //is always true.
 console.log("it's me");
 }
 }

Change it to something like the following using secon variable if you want to loop in the while section.

 for(i=0;i < 2; i++)
 {
 console.log("hello");
 j=0;
 while(j < 2)
 {
 j++;
 console.log("it's me");
 }
 }
answered Mar 22, 2017 at 18:17

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.