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

"Partially" sorting list of POJO

I have a List of objects of the following class: public class Foo { private Date date; private String name; private Long number; } This list is fetched from a database with order by date asc, number desc, but the part that need to be…
XtremeBaumer
  • 6,275
  • 3
  • 19
  • 65
7
votes
15 answers

Is it possible to exit a for before time in C++, if an ending condition is reached?

I want to know if it is possible to end a for loop in C++ when an ending condition (different from the reacheing right number of iterations) is verified. For instance: for (int i = 0; i < maxi; ++i) for (int j = 0; j < maxj; ++j) // But…
tunnuz
  • 23,338
  • 31
  • 90
  • 128
7
votes
2 answers

R memory management - increasing memory consumption

My code looks as follows (it's a little bit simplified version compared to the orginal, but it still reflects the problem). require(VGAM) Median.sum = vector(mode="numeric", length=75) AA.sum = vector(mode="numeric", length=75) …
brunner
  • 107
  • 8
7
votes
2 answers

How to vectorize triple nested loops?

I've done searching similar problems and I have a vague idea about what should I do: to vectorize everything or use apply() family. But I'm a beginner on R programming and both of the above methods are quite confusing. Here is my source…
Nicholas Humphrey
  • 1,220
  • 1
  • 16
  • 33
7
votes
3 answers

Javascript - scope of nested for loop index

I remember variables are function scoped in Javascript. But, how is the behavior if I redefine the local variable in a loop. One common use case is nested loops. In the below code, if I change j to i, the outer for loop terminates after one…
bsr
  • 57,282
  • 86
  • 216
  • 316
7
votes
3 answers

Lisp macro (or function) for nested loops

Is it possible to write a Common Lisp macro that takes a list of dimensions and variables, a body (of iteration), and creates the code consisting of as many nested loops as specified by the list? That is, something like: (nested-loops '(2 5 3) '(i j…
mmj
  • 5,514
  • 2
  • 44
  • 51
6
votes
3 answers

Updating dataframe value based on list

I have a dataframe, based on the strings in a column named "originator" I would like to check if the string has a word that resides in another list. If the string has a word that resides in the said list, update column originator_prediction to…
mikelowry
  • 1,307
  • 4
  • 21
  • 43
6
votes
5 answers

Any way to speedup itertool.product

I am using itertools.product to find the possible weights an asset can take given that the sum of all weights adds up to 100. min_wt = 10 max_wt = 50 step = 10 nb_Assets = 5 weight_mat = [] for i in itertools.product(range(min_wt, (max_wt+1),…
6
votes
1 answer

How to assign same color to factors across plots in a nested loop for ggplot?

I am trying to use scale_fill_manual to assign corresponding colors to factors across many plots in a nested for loop. However, the resulting plots end up all being black. My overall loop is as follows: for(i in seq(from=0, to=100, by=10)){ for{j…
K. Acharya
  • 61
  • 1
  • 2
6
votes
2 answers

Check whether an array contains a value from another array

I have an array of objects, and an array of acceptable return values for a particular method. How do I reduce the array of objects to only those whose method in question returns a value in my array of acceptable values? Right now, I have this: my…
PrincessRTFM
  • 182
  • 10
6
votes
2 answers

Nested Loops (Inner Join) cost 83%. Is any way to rewrite it somehow?

The SP runs very slow. When I look at execution plan - I can see that 83% of its cost goes to Nested Loops (Inner Join) Is any chance to substitute it somehow? Here is my SP ALTER PROCEDURE [dbo].[EarningPlazaCommercial] @State …
Serdia
  • 4,242
  • 22
  • 86
  • 159
6
votes
2 answers

Julia - Continue outer loop

I am currently porting an algorithm from Java to Julia and now I have come across a part where I have to continue an outer loop from an inner loop when some condition is met: loopC: for(int x : Y){ for(int i: I){ …
DP.
  • 581
  • 4
  • 15
6
votes
2 answers

Julia - n-nested loops

Im trying to make a n-nested loop method in Julia function fun(n::Int64) @nloops n i d->1:3 begin\n @nexprs n j->(print(i_j))\n end end But the @nloops definition is limited to _nloops(::Int64, ::Symbol, ::Expr, ::Expr...) and I…
isebarn
  • 3,812
  • 5
  • 22
  • 38
6
votes
5 answers

Several nested 'for' loops, continue to next iteration of outer loop if condition inside inner loop is true

I know it is terribly inefficient and ugly code, but if I have three for loops, nested inside each other such as so: for x in range(0, 10): for y in range(x+1, 11): for z in range(y+1, 11): if ... I want to break the two inner…
KOB
  • 4,084
  • 9
  • 44
  • 88
6
votes
1 answer

Nested WHILE loops in Python

I am a beginner with Python and trying few programs. I have something like the following WHILE loop construct in Python (not exact). IDLE 2.6.4 >>> a=0 >>> b=0 >>> while a < 4: a=a+1 while b < 4: b=b+1 print a,…
Guru
  • 2,331
  • 6
  • 31
  • 48