Questions tagged [nested-loops]

A logical structure used in computer programming where two or more repeating statements are placed in a "nested" form (i.e., one loop is situated within the body of another). The inner loop is repeated in full on every pass through the outer loop.

A nested loop is a loop control structure which contains a (inner) loop within it, aka a loop within a loop.

Nested loops are often used to loop through all the elements of a multidimensional array or a matrix.

Here is an example code demonstrating a nested loop: It enumerates all the pairs (x, y), where x ranges from 0 to 3, and y ranges from 0 to 2, as well as print out when did the outer and inner loop finish.

int x = 0;
int y = 0;
while (x <= 3) {
    while (y <= 2) {
        printf("(%d, %d)\n", x, y);
        y++;
    }
    printf("Inner Loop finished\n");
    x++;
    y = 0;
}
printf("Outer Loop finished\n");

This double loop would run as follows:

(0, 0)                                                                                                                                                         
(0, 1)                                                                                                                                                         
(0, 2)                                                                                                                                                         
Inner Loop finished                                                                                                                                            
(1, 0)                                                                                                                                                         
(1, 1)                                                                                                                                                         
(1, 2)                                                                                                                                                         
Inner Loop finished                                                                                                                                            
(2, 0)                                                                                                                                                         
(2, 1)                                                                                                                                                         
(2, 2)                                                                                                                                                         
Inner Loop finished                                                                                                                                            
(3, 0)                                                                                                                                                         
(3, 1)                                                                                                                                                         
(3, 2)                                                                                                                                                         
Inner Loop finished                                                                                                                                            
Outer Loop finished  
4870 questions
1
vote
2 answers

Loop over three lists simultaneously: nested loop not working

I am currently trying to loop over three lists at the same time: list_weight = [0.9,0.3,0.6,0.4] list_reliability = [0.8,0.5,0.2,0.8] belief_CRED0 = [create_belief_matrix ('ACBA').iloc[0]] belief_CRED0 Out[40]: [1 0.562500 2 0.562500 3 …
Dine
  • 47
  • 8
1
vote
2 answers

Nested 'foreach' for multidimensional array

"XYZ","Age"=>"12"); $a_23 = array("Class"=>"5","Sec"=>"A"); $r_23 = array("Personal"=>$p_23,"Academic"=>$a_23); $p_24 = array("Name"=>"ABC","Age"=>"14"); $a_24 = array("Class"=>"6","Sec"=>"B"); $r_24 =…
1
vote
1 answer

What is a good MVC strategy for generating and debugging reports?

I'm looking to collect tips, blogs, and other best practices on debugging the generation of report data. I'm not looking for reporting classes, many good posts exist here about this already. These won't help in getting the data from the data…
1
vote
1 answer

How to use nested loop for a Matrix cell in excel vba

Current Excel Sheet Structure filteredStartRow = A2 detailsStartRow = D2 ※i = x(get value for filteredItemCount, e.g: 2, 3, 2) ※j = y(get value for detailsItemCount, e.g: 4, 5, 3) I am stuck, how efficiently I can move to second filteredStartRow…
DIO
  • 15
  • 3
1
vote
4 answers

Conditional statements in List comprehension with nested lists(Python 3):

elements = ['121', '9', '55', '5'] I am trying to check if any of the items in elements list is a palindromic number. If there exists any, return True(else False). I tried implementing by using map + lambda: here is the…
Rajesh M
  • 31
  • 7
1
vote
3 answers

A better way than having 3 nested for loops to get data I need

I can build a dictionary with the way I'm curretnly doing but I was wondering if there was a better and faster way. I'm trying to use LINQ SelecMany statement but having trouble. var replyChildren = reply.Children[0]; Dictionary slowLog…
jjss
  • 93
  • 7
1
vote
2 answers

Fixing a 4 nested for loops in Python

So i'm trying to implement the agglomerative clustering algorithm and to check the distances between each cluster i use this: a, b = None, None c = max for i in range(len(map)-1): for n in range(len(map[i])): for j in range(i+1,…
Mina F
  • 47
  • 2
  • 8
1
vote
4 answers

JavaScript - Join arrays of an array with concat method instead of push method

I have a problem that asks me to join arrays of an array and return a single array in the form of [ array[0][0], array[0][1], array[1][0], array[1][1], etc. ]. I solved it using the push method in nested for-loops, but the prompt says that I should…
Michael
  • 25
  • 3
1
vote
4 answers

How to break out of an inner loop back to an outer loop?

I have 4 entities" ObjectA, ObjectB, ObjectC, ObjectD foreach(ObjectA objectA in listObjectA) if (relationAB) foreach(ObjectB objectB in listObjectB) if (relationBC) foreach(ObjectC objectC in listObjectC) if (relationCD) …
Aaaron
  • 11
  • 3
1
vote
3 answers

PHP- How to solve else in nested foreach loop

This my code syntax all conditions are working but last else is not working. I found solution in there https://stackoverflow.com/a/5930255/7688968 and I am used break 2 but still not working. How can I solve that? get_results(…
Sakib
  • 126
  • 20
1
vote
3 answers

What is a nested loop and how do I use it in example below?

I was given the following problem for my beginning python coding class. I have reached out to the professor, but he hasn't gotten back to me and this is due in 3.5 hours. If someone has a tutorial/guidance of how to start, that would be amazing. I…
Michelle McGuire
  • 77
  • 2
  • 3
  • 12
1
vote
2 answers

Time complexity analysis in finding index of smallest element in an array-worst,average and best case

int j = 0; for(int i = 0; i < n; ++i) { while(j < n && arr[i] < arr[j]) { j++; } } Here is a code for finding index of smallest element (first occurrence) in an array.And i say that the worst case would be O(n2) best case would be…
Sai Shankar
  • 61
  • 1
  • 8
1
vote
1 answer

Nested for loop in django template not showing correct output

So i have this loop which is showing the correct output when i print it in my views.py file for x in list4: print x[0] for y in x[3]: print y[1] print "\n" output: test question edited test1 test2 test3 test…
Bhawesh Chandola
  • 511
  • 5
  • 19
1
vote
2 answers

Batch nested loop doesn't work

I'm trying to make a mod loader for a game in batch but I can't get it to work with a nested loop. The command used is this loadMods.bat mod1.txt mod2.txt ... modN.txt This is the code I'm using setlocal EnableDelayedExpansion set /p…
Teodor Vecerdi
  • 326
  • 1
  • 2
  • 13
1
vote
3 answers

R: How can I cbind specific columns of all data frames of a nested loop within the loop?

I am trying to combine the third column of several data frames, which are called and renamed in a nested for loop, within the same looping process. # Sample Data ecvec_msa6_1998=matrix( round(rnorm(200, 5,15)), ncol=4) ecvec_msa6_1999=matrix(…
BeSeLuFri
  • 623
  • 1
  • 5
  • 21