-1

How do I use while loop inside while loop without executing the next loop (from outside while loop) unless the inner while loop done executing?

var x = 0;
var y = 0;
while(x < 10){
   while(y < 10){
      console.log(y);
      y++;
   }
  x++;
}
7
  • 2
    I've got trouble understanding what you are asking. What is supposed to be the outcome of your code there? Commented Nov 17, 2016 at 17:26
  • put y=0; before x++ Commented Nov 17, 2016 at 17:26
  • That's what it does now; the entire inner loop will run before the next iteration of the outer loop. What's your specific issue? Not resetting the y counter value? Commented Nov 17, 2016 at 17:27
  • not sure what you're asking but i bet the answer is either break; or continue; Commented Nov 17, 2016 at 17:27
  • ex. the outer loop execute first, then the second loop execute too. but before the outer loop iterate again through x++, the inner loop should finish looping first. Commented Nov 17, 2016 at 17:28

2 Answers 2

3

I'm guessing your problem here is that at the end of the inner loop, y is set to 10 and it never is reset to 0. Try this instead:

var x = 0;
var y = 0;
while(x < 10){
   while(y < 10){
      console.log(y);
      y++;
   }
  y = 0;
  x++;
}

Otherwise, once the inner loop finishes once, it never runs again.

Sign up to request clarification or add additional context in comments.

1 Comment

Just a pointer, instead of setting y=0 after loop, move declaration of y inside loop as var y = 0.
0

Put y initialization inside first while loop.

var x = 0;
while(x < 10){
   var y = 0;
   while(y < 10){
      console.log(y);
      y++;
   }
   x++;
}

1 Comment

Yah maybe that was what he wanted

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.