+ 13

Simpler nested for loop question

Here is an example of nested for loops without iterating through an array. Could someone please walk me through this? Thanks for any help, I'm stuck. I would also like to know what this structure can be used for. Thank you all. var count = 0; for (var i =0;i<3; i++) { //this is a for loop starting at 0 and ending at 2 incrementing by 1 for (var j=0; j <=2; j++) { //this is a nested for loop from 0 to 2 incrementing by 1 count++; //increment count by 1 } } document.write(count);

8th Mar 2017, 2:02 PM
‎‏‎‏‎Joe
‎‏‎‏‎Joe - avatar
3 Antworten
+ 13
thank you!
8th Mar 2017, 4:46 PM
‎‏‎‏‎Joe
‎‏‎‏‎Joe - avatar
+ 1
this will be used to tell how many times this loop is running. here count should be : 3*3 =9
8th Mar 2017, 2:21 PM
Raj Kumar Chauhan
Raj Kumar Chauhan - avatar
+ 1
We begin with the outter loop (i) starting at 0, the conditional (i < 3) Is true so, like an if statement, it steps inside and executes the next code before anything else. In this case, we move to an inner loop (j). The inner loop will then iterate until conditional (j < 3) is false and it completes one loop by moving back to your i loop and incrementing the i. Now it repeats, with i = 1. If you output the variables you will see exactly how the count goes (ex:0 012 1 012 2 012)
8th Mar 2017, 6:38 PM
Bobby
Bobby - avatar