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
11
votes
8 answers

Creating a Christmas Tree using for loops

I am trying to make a christmas tree using for loops and nested for loops. For me to do that I need to be able to make a pyramids with *. I have tried countless times and I am having problems making one. Here is my code: for(int i=1;i<=10;i++){ …
Atif Shah
  • 183
  • 1
  • 1
  • 10
11
votes
1 answer

Java Lambda Expression for Nested Loops with Conditional

I am new to lambda expressions and am trying to use them to reduce the following code to the lambda equivalent. I have looked into reduce and flatMap and forEach, as well as several other things, but I am obviously missing something because…
Todd
  • 1,895
  • 4
  • 15
  • 18
11
votes
2 answers

Itertools equivalent of nested loop "for x in xs: for y in ys..."

I have a nested loop to create all combinations in a set of conjugated verbs. The aim to to get all possible combinations of verb, person and tense, e.g. [['to be', 'first person singular', 'future'],['to be', 'second person singular', 'future'],…
Jamie Bull
  • 12,889
  • 15
  • 77
  • 116
11
votes
3 answers

PHP Break & Continue at the same time

I have a nested loop like so: while(1){ while($something){ break & continue; } // More stuff that I don't want to process in this situation } I want to break out of the 2nd while loop and continue the 1st loop from the start…
nick
  • 2,743
  • 4
  • 31
  • 39
10
votes
1 answer

Efficiently compute histogram of pairwise differences in a large vector in R?

I am working with a large vector of integers in R (around 10 million integers), and I need to find every distinct pair of integers from this vector that differ by 500 or less and make a histogram of their differences (i.e. for each pair, the second…
Ryan C. Thompson
  • 40,856
  • 28
  • 97
  • 159
10
votes
13 answers

Break nested loops

Can someone tell me how to break the main loop when I have nested loops? Example*: /*Main loop*/ for(int y = 0; y < 100; y+=10) { /*Sub loop*/ for (int x = 0; x < 100; x += 10) { if(x == 60) { //Break the…
Bankin
  • 777
  • 1
  • 16
  • 31
10
votes
1 answer

Multi-dimensional nested OpenMP loop

What is the proper way to parallelize a multi-dimensional embarrassingly parallel loop in OpenMP? The number of dimensions is known at compile-time, but which dimensions will be large is not. Any of them may be one, two, or a million. Surely I…
user657199
10
votes
2 answers

Mutating an item inside of nested loops

I'm trying to write a program to evenly (or as close to possible) distribute points about the surface of a sphere. I'm trying to achieve this by placing N points randomly about a unit sphere, and then running multiple steps where the points repel…
George Roger
  • 213
  • 3
  • 9
10
votes
4 answers

Efficiency of nested Loop

See the following snippet: Long first_begin = System.currentTimeMillis(); // first nested loops for (int i = 0; i < 10; i++) { for (int j = 0; j < 1000000; j++) { // do some stuff } } …
didxga
  • 5,935
  • 4
  • 43
  • 58
10
votes
1 answer

How is Loop skewing making loop parallelizable?

I am reading about loop tranformation techniques and i have a very hard time understanding how does loop skewing makes a loop parallelizable Here are are two loops, the initial one and the seconde one, what is the difference betwwen the two ? The…
user2133558
  • 524
  • 2
  • 10
  • 29
10
votes
3 answers

Python nested loop with generators does not work (in some cases)?

Would somebody please explain the behavior of a nested loop using generators? Here is an example. a = (x for x in range(3)) b = (x for x in range(2)) for i in a: for j in b: print (i,j) The outer loop is not evaluated after the first…
phantomile
  • 103
  • 1
  • 5
9
votes
3 answers

Mutate across multiple columns to create new variable sets

I have a panel dataset at the country and year level, and I'd like to create a two new variables based on existing ones. year country var1 var2 var3 var…
PierreRoubaix
  • 167
  • 1
  • 1
  • 7
9
votes
3 answers

How to run a nested loop in python inside list such that the outer loop starts from the next element of the list always and so on

I have a list something like: [[16777230, 0], [16777226, 1], [16777252, 2], [16777246, 0]] I want to make a loop inside a loop(nested loop) for my operation in python such that the inner loop will always start from the next element of the outer…
JOSEPH Blessingh
  • 157
  • 2
  • 10
9
votes
3 answers

g++ optimization breaks for loops

A few days ago, I encountered what I believe to be a bug in g++ 5.3 concerning the nesting of for loops at higher -OX optimization levels. (Been experiencing it specifically for -O2 and -O3). The issue is that if you have two nested for loops, that…
Lilith Daemon
  • 1,473
  • 1
  • 19
  • 37
9
votes
2 answers

Big-O complexity of nested for loops

I'm confused about the complexity of the following (the operation performed inside the inner loop is in constant time): for(int i=0; i
Riz
  • 6,486
  • 19
  • 66
  • 106