+ 5

Can I have a for loop in a for loop?

I just want to know if its possible to have a for loop in another for loop?

22nd Oct 2018, 3:51 PM
Nikola Iliev
Nikola Iliev - avatar
10 Respuestas
+ 18
If you are talking about loop inside another loop then yes its called Nesting-Loops for(initialization; condition; increment/decrement) { for(initialization; condition; increment/decrement) { .... } }
22nd Oct 2018, 4:10 PM
blACk sh4d0w
blACk sh4d0w - avatar
+ 8
They are called nested loops, and very commonly used in practice. E.g. iterating through a 2D array/matrix. int arr[3][3] = {{1,2,3},{4,5,6},{7,8,9}}; for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) std::cout << arr[i][j] << " ";
22nd Oct 2018, 4:09 PM
Hatsy Rei
Hatsy Rei - avatar
+ 5
For the future: When that kind of question comes up, just go to the Playground and try it yourself! Like that you learn the most since you will much easier remember something you have done with your own hands.
22nd Oct 2018, 6:58 PM
HonFu
HonFu - avatar
+ 3
Thanks. :)
22nd Oct 2018, 4:12 PM
Nikola Iliev
Nikola Iliev - avatar
+ 2
yes there can be nested for loops
23rd Oct 2018, 3:25 AM
Shreyas Shreyas
Shreyas Shreyas - avatar
+ 1
Yes it is possible. We call it nested for loops and is very useful toward 2D arrays (Java) and basic bubble sorting.
23rd Oct 2018, 4:44 PM
Virus5600
Virus5600 - avatar
+ 1
yes you can in python for ... : for ... : bb for ... :
24th Oct 2018, 12:13 AM
ducky
ducky - avatar
+ 1
Yes nested for loops
25th Oct 2018, 12:30 AM
SARAH
+ 1
yes you can have and those are call nested loops example of nested for loop in c for ( init; condition; increment ) { for ( init; condition; increment ) { statement(s); } statement(s); }
25th Oct 2018, 7:13 AM
Dhrub Raj Giri
Dhrub Raj Giri - avatar
0
Yes....Its absolutely legitimate....if you wanna go through example the nested for loop in used in matrix multiplication
6th Nov 2018, 11:39 AM
Vaishnavi Deo
Vaishnavi Deo - avatar