+ 1

How do nested for loops work?

2nd Apr 2019, 9:53 PM
Juan David Padilla Diaz
Juan David Padilla Diaz - avatar
6 Antworten
+ 3
Which language? One example would be a two-dimensional array, which is filled with data. The first loop goes through the first index and the second loop goes through the second index. Something like this: FOR (START; END; INCREMENT) FOR (START; END; INCREMENT) ... some code ... END FOR END FOR Here is an example to fill a two-dimensional array with numbers in Javascript: for (let i=0; i<8; i++) { for (let j=0; j<8; j++) { arr[i][j] = i + "-" + j; } }
2nd Apr 2019, 10:38 PM
Pete Wright
Pete Wright - avatar
+ 1
Basically like numbers. Try this (example in Python): for i in range(10): for j in range(10): print(str(i)+str(j))
2nd Apr 2019, 10:29 PM
HonFu
HonFu - avatar
+ 1
I prefer the example with js or c++
2nd Apr 2019, 10:39 PM
Juan David Padilla Diaz
Juan David Padilla Diaz - avatar
2nd Apr 2019, 11:00 PM
Pete Wright
Pete Wright - avatar
+ 1
To illustrate: while(true){ while(true){ while(true){ std::cout<<"hi"; }}} is the same as: while(true){ std::cout<<"hi"; } this can be observed if you step through every line of code using an IDE such as Visual Studio.
3rd Apr 2019, 12:38 AM
Sajjaad Ramdath
0
In c++: for(int i=0;i<9;i++){ for(int i=0;i<9;i++)std::cout<<i; } This prints: "0123456789" 10 times. The innermost loop executes before the outer executes.
2nd Apr 2019, 11:57 PM
Sajjaad Ramdath