0
Anyone plz explain me the logic behind nested for loops..
i am getting how the second for loop is working here ... for example, #include<iostream> using namespace std; int main; { for(a=1; a<10 ; a++) { for(b=1; b<10; b++) { cout<<"♧";} } return 0;}
3 ответов
+ 7
When the first loop is running its first iterarion, it encounters a second loop. The second loop must run all the way through before the first loop's first iteration can be completed.
Then, on the first loop's second iteration, it hits the second loop again, goes through it completely, and then finishes the first loop's iteration. It repeats until the first loop is completely done.
So it's like counting in this fashion: 1 (1, 2, 3), 2 (1, 2, 3), 3 (1, 2, 3), etc.
+ 1
Imagine a table with rows and columns. Now, imagine a program that will count each column in a row. So, the program must iterate all rows and columns.
Suppose its a 3x3 table. So, 3 rows and 3 columns. Nesting a for loop would make the program count each column in each row:
Row 1 - Column 1
Row 1 - Column 2
Row 1 - Column 3
...
Row 2 - Column 1
Row 2 - Column 2
Row 2 - Column 3
...
Row 3 - Column 1
Row 3 - Column 2
Row 3 - Column 3
Hope I explained clearly xD
0
when getting confused by loop iterations perceive like this,
multiply the no of repetitions for each loop to get the total no of repetitions.
for ur case
1st for loop runs 9 times
{ 2nd for loop runs 9 times
{ so 🌳 is printed 9x9 =81 times}
}