+ 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?
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)
{
....
}
}
+ 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] << " ";
+ 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.
+ 3
Thanks. :)
+ 2
yes there can be nested for loops
+ 1
Yes it is possible. We call it nested for loops and is very useful toward 2D arrays (Java) and basic bubble sorting.
+ 1
yes you can in python for ... :
for ... :
bb for ... :
+ 1
Yes nested for loops
+ 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); }
0
Yes....Its absolutely legitimate....if you wanna go through example the nested for loop in used in matrix multiplication