Tell me why the nested loop for each value of the variable i is executed nine more times?

for (var i = 1; i < 10; i++) { for (var j = 1; j < 10; j++) { document.write(j); } document.write(i + "<br>"); } 
  • 3
    And what result did you expect? - ReinRaus
  • 3
    Do not be lazy to read something about cycles. The question is meaningless. - karmadro4
  • 2
    @adidassler, I don’t know, I don’t know ... The code is executed exactly as you wrote, and this is obvious to everyone except you. It may be worthwhile to edit the question and reveal what exactly is not clear, what you expected, etc. - karmadro4
  • 2
    Perhaps the author needs to practice on something simple, console ... m. basic? - avp
  • one
    Of course simpler, BASIC was designed for introductory training in algorithmic languages. - karmadro4

1 answer 1

Because it is a nested loop.

Added from comment.

The code that is inside the loop is executed a specified number of times, in your case, 9 times. Accordingly, the nested loop will also be executed 9 times, and the code inside it will be 9 * 9 total 81 times, logically read the code: stumble upon the first loop, do the first pass, stumble upon yet another loop, pass nine passes through it, then we output i, then we return to the beginning of the cycle for the second pass and so 9 times.

  • but for more meaningful how can you explain!? - adidassler
  • one
    Moved in response. - FLK