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
9
votes
7 answers

Is a big loop within a small loop always faster than a small loop within a big one?

I just read this post, and wonder if we can draw the conclusion that a big loop within a small loop must always run faster than a small loop within a big one, no matter what the code does inside the nested loop? Take an example. int m, n; m =…
Wallace
  • 561
  • 2
  • 21
  • 54
9
votes
3 answers

How to nest multiple parfor loops

parfor is a convenient way to distribute independent iterations of intensive computations among several "workers". One meaningful restriction is that parfor-loops cannot be nested, and invariably, that is the answer to similar questions like there…
s.bandara
  • 5,636
  • 1
  • 21
  • 36
9
votes
1 answer

Data transformation avoiding nested loops in R

I have a contingency table data matrix with 6 columns and 37 rows. I need to apply a Chi squared transformation to give me Row profiles and Column profiles for a correspondence analysis. Unfortunately I've been told I will need to use nested loops…
Confused
  • 91
  • 3
8
votes
3 answers

Better equivalent of this crazy nested python for loop

for a in map: for b in map[a]: for c in map[b]: for d in map[c]: for e in map[d]: print a+b+c+d+e The above code is used to create all paths of certain length in a graph. map[a] represents…
Babak
  • 582
  • 4
  • 14
8
votes
1 answer

How to break out of nested loops in Dart

Given the following nested loop: for (int i = 0; i < 10; i++) { for (var j = 0; j < 10; j++) { print('i: $i, j: $j'); } } If I add a break statement to the inner loop: for (int i = 0; i < 10; i++) { for (var j = 0; j < 10; j++) { …
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
8
votes
1 answer

Can you help me with a symbol-state table and nested switch? Exercise from Illustrating C- Donald Alcock

OK, first thank you for taking the time to read my post!! (^o^)/ And before I put the whole problem a little of context: I am learning by myself 'C' and found the book "Illustrating C" which I´m working. In his book, Donald Alcock used a…
8
votes
5 answers

Trying to optimize my code to either remove nested loop or make it more efficient

A friend of mine takes a sequence of numbers from 1 to n (where n > 0) Within that sequence, he chooses two numbers, a and b He says that the product of a and b should be equal to the sum of all numbers in the sequence, excluding a and b Given a…
Ashton Hauser
  • 111
  • 10
8
votes
7 answers

Replace Nested For Loops... or not

I have a script that loops through a series of four (or less) characters strings. For example: aaaa aaab aaac aaad If have been able to implement it with nested for loops like so: chars = string.digits + string.uppercase + string.lowercase for a…
Matt in PA
  • 533
  • 7
  • 11
8
votes
2 answers

Advanced Custom Fields display last three sub-repeater rows

I am using Advanced Custom Fields (ACF) to pull repeater information from an events page and displaying a shortened list of the events on the home page. I have set up a repeater to allow the user to input which month the event will take place…
8
votes
1 answer

Is there a way to use itertools in python to clean up nested iterations?

Let's say I have the following code: a = [1,2,3] b = [2,4,6] c = [3,5,7] for i in a: for j in b: for k in c: print i * j * k Is there a way I can consolidate an iterator in one line instead of making it nested?
Skorpius
  • 2,135
  • 2
  • 26
  • 31
8
votes
4 answers

Nested loop construction

This is part of my homework. All I need is a bit of advice. I need to write some nested loop constructs, to print the following: "122333444455555" "+**+++****+++++" "--***++++-----******+++++++" Here is my code to print the first set of…
Hustl3r28
  • 211
  • 7
  • 16
8
votes
3 answers

efficient algorithm instead of looping

I have a data set where each samples has a structure similar to this X=[ [[],[],[],[]], [[],[]] , [[],[],[]] ,[[][]]] for example: X=np.array([ [ [1,2,3], [2,4,5] ,[2,3,4] ] , [ [5,6], [6,6] ] , [[2,3,1],[2,3,10],[23,1,2],[1,4,5]] ]…
Moj
  • 6,137
  • 2
  • 24
  • 36
8
votes
3 answers

Is there problems with nesting many while loops?

I am working on some homework and wanted to know if there is such as thing as too many nested while loops. Is there downsides to nesting several while loops? If so how would one refactor the code snippet I have below? Below is code to read a file…
Peter
  • 229
  • 4
  • 10
7
votes
3 answers

How to determine the sum of a group of integers without using recursion

This is my first post on Stack Overflow, and I'm hoping that it'll be a good one. This is a problem that I thought up myself, and now I'm a bit embarrassed to say, but it's beating the living daylights out of me. Please note that this is not a…
7
votes
2 answers

Reuse nested loops without copy and paste

Suppose I've this nested loop for (int a=1; a
Ruggero Turra
  • 16,929
  • 16
  • 85
  • 141