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
1
vote
2 answers

In c++, c why conditions inside for loop block and within for loop acts differently?

A condition inside for loop and a same condition inside for loop block. Why these 2 gives different output? for (i=0;i <5;i++) { printf("\n"); for (j=0;j <5;j++) if (i!=j) printf (" i= %d j= %d ",i,j); …
kaushikC
  • 77
  • 3
1
vote
0 answers

nested for loop not working properly in java

[this is for 4x4 matrix] [this one is for 3x4 matrix] import java.util.Scanner; public class MyClass { public static void main(String args[]) { int num[][]= new int [4][4]; Scanner…
ALi Raza Darr
  • 149
  • 1
  • 1
  • 9
1
vote
1 answer

Javascript nested for loop and array.reduce

I am a newbie in programming and new to this site and I have a question about nested for loops. This is the code: function eddardStarkSecret() { var ans =[]; var map = [ [111, 1, 6], [110, 2, 5, 22], [115, 10], [83, 4], [119, 7], …
1
vote
2 answers

Shell:nested for loop

Somehow in the following script, value of $i is not expanded on line 3. Any idea why? for i in `cat test.txt` do for j in `find . -name $i` do echo $j done done
simplfuzz
  • 12,479
  • 24
  • 84
  • 137
1
vote
1 answer

nodejs Async.each Nested loop Confusion

i want to have two nested for loops async.each(ListA, function(itemA,callback1){ //process itemA async.each(itemA.Children, function(itemAChild,callback1){ //process itemAChild callback1(); …
DrVishnu
  • 123
  • 1
  • 3
  • 10
1
vote
2 answers

String Translate Robber's Language

message="this is fun" def translate(robber): consonants=['bcdfghjklmnpqrstvwxz'] for letters in robber: if letters in consonants: return (letters + 'o' + letters) continue else: return…
1
vote
1 answer

How to perform a nested Thymeleaf loop to create a table containing fields from two JPA table classes

I'm trying to create a web page using a thymeleaf template to present a table of Orders with a field that provided a list of products associated with a specific order. My controller class: @Controller public class WebPage { @Autowired …
john
  • 31
  • 1
  • 4
1
vote
2 answers

My nested loops not working

I'm trying to match inputted text with a list of words from a pre-defined array. However, it's not returning anything, even the console.log doesn't return anything. I can't work out why the out loop isn't initiating. Any help would be greatly…
Cuckoo
  • 356
  • 1
  • 3
  • 14
1
vote
1 answer

Halide sum loops nesting

Printed loops nesting for Halide::sum is not equivalent for optimal as written in tutorial. This code provides separate loops for zero initialization and summation. Halide::Func f("f"); Halide::Var x("x"); Halide::RDom r(0, 3); f(x) =…
Dmitry Kurtaev
  • 823
  • 6
  • 14
1
vote
1 answer

R Shiny-->Reactive Environment Errors with nested loops

All, I am working on the following code. Takes some inputs from two files (portfolio and blend) and does some sorting/ functions, then runs a monte carlo-esque simulation into a linear program. I Think everything is set up correctly, but I am…
MDEWITT
  • 2,338
  • 2
  • 12
  • 23
1
vote
2 answers

Building JSON with java overwrites values

I'm trying to represent some info from the database with a json, the json structure is created well but don't know why overwrittes the last record from the database. So on my json I just got the last value readed instead al the values. To do this I…
jcobo1
  • 1,065
  • 1
  • 18
  • 34
1
vote
0 answers

Angular2 and Angularfire nested *ngfor issues

Component export class Component{ people: Observable; constructor(public af:AngularFire) { this.people = af.database.list('/people') .map((people) => { return people.map((person) => { person.post =…
1
vote
0 answers

Combining objects in nested loops with big data

I'm trying to combine array objects to strings so that I get many combinations of results.(3000+) However, this following code does work, but it is insufficient/slow. It also tends to freeze. I have tried with for loops as well but i get the same…
BlitZz
  • 132
  • 1
  • 14
1
vote
2 answers

Nested loop efficiency in c

Which is better way and Why? Case 1: for(i=0; i< 100; i++) for(j=0; j< 10; j++) printf("Hello"); case 2: for(i=0; i<10; i++) for(j=0; j< 100; j++) printf("Hello");
beparas
  • 1,927
  • 7
  • 24
  • 30
1
vote
1 answer

Nested data from an Rdd Scala spark

My sample data looks like below { Line 1 Line 2 Line 3 Line 4 ... ... ... Line 6 Complete info: Dept : HR Emp name is Andrew lives in Colorodo DOB : 03/09/1958 Project name : Healthcare DOJ : 06/04/2011 DOL : 09/21/2011 Project name : Retail DOJ…
user7264473
  • 163
  • 1
  • 10
1 2 3
99
100