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
5
votes
3 answers

Cross join two lists java

I have a class ABC which contains two integer fields public class ABC{ private Integer x; private Integer y; // getters and setters } I have two lists : xValues and yValues, which contain list of integers of x and y values…
Akash Raveendran
  • 559
  • 1
  • 9
  • 22
5
votes
2 answers

Update values in a Nested Dictionary in Python

I have a nested python dictionary like this { "node":{ "node1":{ "node1.1":{ }, "node1.2":{ } }, "node2":{ "node2.1":{ "node2.1.1":{ } } } } } What I am trying to implement…
Ajai
  • 901
  • 9
  • 21
5
votes
2 answers

Variable number of predictable for loops in Python

I am trying to come up with a way to generate all possible unique strings from an alphabet of 20 characters where the order within the string doesn't matter, and the length of the string can vary. So, for instance, for a string of length 3, the…
LordHadron
  • 53
  • 4
5
votes
1 answer

Dynamically adding nested loops

I have an 'X' amount of variables (likely to range between 3 to 20 options), which will be combined to calculate all possible combinations to meet a criteria. For every extra variable an extra loop is introduced, however I do not know if it possible…
Paul W
  • 83
  • 2
5
votes
1 answer

ColdFusion - Looping through a nested structure in an array

I have a json retuned from an external API: { "data" : { "consignmentDetail" : [ { "consignmentNumber" : "5995600864", "parcelNumbers" : [ "15505995600864" ] } ], "consolidated" : false, "shipmentId" : "60764454" …
jscar
  • 53
  • 1
  • 5
5
votes
1 answer

Creating a Map using Java8 streams on a nested Data Structure

I am searching for an elegant equivalent of this piece of code using Java 8's streams: Collection xs = ...; Map map = new SomeMap<>(); for (X x : xs) { A a = x.getA(); Collection bs = x.getBs(); for (B b : bs) …
good_weather
  • 53
  • 1
  • 4
5
votes
2 answers

too many statically nested blocks python

I'm trying to write more than 21 lists containing the same number of items to columns in a text file. import random a=[] b=[] .... q=[] for i in range(200): a.append(random.gauss(10,0.1)) b.append(random(20,0.5)) .... …
Josh J
  • 395
  • 1
  • 3
  • 13
5
votes
4 answers

C++ Optimize vectorizing nested loops

I have a program dealing with multiple nested loops, operating on a 3D domain: unsigned int sX(m_sizeZ*m_sizeY); unsigned int b(sX+m_sizeZ); for(unsigned int i(1);i
repptilia
  • 457
  • 8
  • 19
5
votes
3 answers

How to generalize a sequential COUNT() of chronological data without loops or cursors?

I have read all the arguments: Tell SQL what you want, not how to get it. Use set-based approaches instead of procedural logic. Avoid cursors and loops at all costs. Unfortunately, I have been racking my brain for weeks and I can't figure out how to…
5
votes
4 answers

Breaking out of nested loops: return or label/break?

I am using a JavaScript function to set a global variable. Below, I have two really dumb example functions. One uses a label to break out of the nested loops. The other uses an empty return. My question: which is better from a performance issue?…
Matthew Herbst
  • 29,477
  • 23
  • 85
  • 128
5
votes
1 answer

R - multiple nested loops

I am trying to write a nested loop code to simulate 10 columns of data in a data frame with 101 rows. The first row of data has been assigned as starting values. Each column should be different as my matrix r is generated from random normals;…
Tiffany
  • 301
  • 1
  • 2
  • 12
5
votes
5 answers

Java Nested Loop Initialization

I am new at java, so there is some piece of code in nested loop I don't understand... for(int i=0; i<3; i++){ for(int j=1; j<3; j++){ System.out.println(i + "" + j); } } it will run: 01,02,11,12,21,22 but when I changed into this: int…
5
votes
2 answers

Near empty Java For-Loop acts strange

This code acts as expected printing "Average Number of Runs: 0.99864197" import java.util.Random; public class A { public static void main(String[] args) { int min = -30; int max = 1; test(min, max); } static…
Zaide Chris
  • 361
  • 1
  • 8
5
votes
2 answers

Breaking out of an outer loop from an inner loop in javascript

while(valid){ for(loop through associative array){ if(!valid){ break; } } } I have tried to find a way to break out of the while loop from the if statement. So far, the best method seems to be the goto method that is…
DitR
  • 123
  • 1
  • 10
5
votes
2 answers

Passing argument from Parent function to nested function Python

here is my code: def f(x): def g(n): if n < 10: x = x + 1 g(n + 1) g(0) When I evaluate f(0), there would be an error "x referenced before assignment". However, when I use "print x" instead of "x = x + 1" ,…
octref
  • 6,521
  • 7
  • 28
  • 44