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

Parallelize these nested for loops in python

I have a multidimensional array (result) that should be filled by some nested loops. Function fun() is a complex and time-consuming function. I want to fill my array elements in a parallel manner, so I can use all my system's processing…
Amir Masoud
  • 341
  • 1
  • 6
  • 17
14
votes
6 answers

Why can't I add a goto label at the end of a method?

After researching a way to exit a nested loop, I decided to try using goto, private void example() { for (int i = 0; i < 100; i++) { for (int ii = 0; ii < 100; ii++) { for (int iii = 0; iii < 100; iii++) …
Sam
  • 7,252
  • 16
  • 46
  • 65
13
votes
4 answers

Is this a valid (ab)use of lambda expressions?

Like we all know, it's not that easy to break from a nested loop out of an outer loop without either: a goto (Example code.) another condition check in the outer loop (Example code.) putting both loops in an extra function and returning instead of…
Xeo
  • 129,499
  • 52
  • 291
  • 397
13
votes
2 answers

Java 8 nested loops with streams & performance

In order to practise the Java 8 streams I tried converting the following nested loop to the Java 8 stream API. It calculates the largest digit sum of a^b (a,b < 100) and takes ~0.135s on my Core i5 760. public static int digitSum(BigInteger x) { …
Konrad Höffner
  • 11,100
  • 16
  • 60
  • 118
13
votes
4 answers

Big O for 3 nested loops

A Big O notation question...What is the Big O for the following code: for (int i = n; i > 0; i = i / 2){ for (int j = 0; j < n; j++){ for (int k = 0; k < n; k++){ count++; } } } My…
Kailua Bum
  • 1,368
  • 7
  • 25
  • 40
13
votes
4 answers

break in do while loop

What happens when breaking in nested loops? suppose the following code: for(int x = 0; x < 10; x++) { do { if(x == 4) break; x++; } while(x != 1); } Which loop will exit on encountering the break statement, the…
sgupta
  • 1,214
  • 2
  • 15
  • 29
12
votes
4 answers

Modify solution to use a single loop

I managed to solve this, below is my solution : public class ProblemA001k { public static void main(String[] args) { System.out.println("Sum from 1" + " to " + divQ + ":" + sum2); System.out.println(); divQ += q; …
11
votes
1 answer

How to make a IEnumerable method parallel method

Following to this post, I want parallelize this method : public IEnumerable GetAllLogs(IEnumerable computers) { foreach (var cpt in computers) { foreach (var log in cpt.GetLogs()) { …
Florian
  • 4,507
  • 10
  • 53
  • 73
11
votes
1 answer

Terraform - iterate over nested map

I am trying to create IAM binding for Bigquery dataset using the resource - google_bigquery_dataset_iam_binding. The requirement is I read the parameters in this resource (dataset_id, role, members) using a variable of the following structure - …
mb_96
  • 127
  • 1
  • 2
  • 9
11
votes
4 answers

How can I avoid writing nested for loops for large data sets?

For a two-variable problem, outer is most likely the best solution for this and if the space to loop over is small enough then we can have expand.grid do our legwork. However, those are ruled out if we have more than two variables and a large space…
J. Mini
  • 1,868
  • 1
  • 9
  • 38
11
votes
3 answers

Exit from nested loops at desired level

Possible Duplicate: Breaking out of a nested loop How to exit from nested loops at a specific level. For example: foreach (item in Items) { foreach (item2 in Items2) { // Break; => we just exit the inner loop // …
Xaqron
  • 29,931
  • 42
  • 140
  • 205
11
votes
4 answers

How to make nested for loop more Pythonic

I have to create a list of blocked users per key. Each user has multiple attributes and if any of these attributes are in keys, the user is blocked. I wrote the following nested for-loop and it works for me, but I want to write it in a more pythonic…
zenprogrammer
  • 623
  • 1
  • 7
  • 20
11
votes
8 answers

How do I design an int loop that starts with 1 and ends with 0 (1,2,3,4,5,6,7,8,9,0)

My problem is to use nested for loops in order to create this output: | | | | | | 123456789012345678901234567890123456789012345678901234567890 I can't figure out the best way to replace the int 10…
JP Wile
  • 156
  • 3
  • 11
11
votes
1 answer

How to loop in Ansible $var number of times?

I want to run a loop in Ansible the number of times which is defined in a variable. Is this possible somehow? Imagine a list of servers and we want to create some numbered files on each server. These values are defined in vars.yml: server_list: …
jwbensley
  • 10,534
  • 19
  • 75
  • 93
11
votes
1 answer

Convert classic nested for loop with Java 8 streams

I want to convert the following code using the Java 8 stream API List deck = new ArrayList<>(); for (Suit s: Suit.values()) { for (Rank r: Rank.values()) { deck .add(new Card(r, s)); } } I came out with this List
Francesco
  • 857
  • 1
  • 11
  • 26