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
1 answer

What is wrong with my looping?

public class Prices { public static void main(String[] args) { int i = 100; int z = 0; int x = 0; int a = 0; int v = 0; double sum = 0; double[][] MovingAvgArray = new double[i][i]; double[] List = {20.19,…
Origomar
  • 11
  • 1
1
vote
3 answers

Python nested loop from files

I have the following code: inputActionFile = '../action.txt' inputDaerahFile = '../daerah.txt' inputStuffFile = '../stuff.txt' inputTermsFile = '../terms.txt' outputFile = 'hasil.out' inputAction = open(inputActionFile, 'r') inputDaerah =…
1
vote
2 answers

Excel VBA - Run through multiple row, if a row is blank, enter a section of headers

I'm writing a macro to sort through a large file of data at work. I've inserted a blank row at the top of different section of data. I want my code to realize when a row is blank in column C, then fill in a set of headers in that row. It should then…
N. Audet
  • 15
  • 5
1
vote
1 answer

How to create an "X" pattern?

I want to know how to create the following pattern using * and _ using nested loops. *_______* __*___* ____* __*___* *_______* I am currently trying to do this in Java, but an answer in C will do as well, I can extrapolate the answer. Here is…
DannyBoi
  • 636
  • 1
  • 7
  • 23
1
vote
1 answer

Nested print in a nested loop in python

How can I create outputs created in two for loops? What I would like to have: Name1 Adress1 Name2 Adress2 .. What I get: Name1 Name2 Adress1 Adress2 I have already tried a lot and brought back to the initial situation. The names and the…
madik_atma
  • 787
  • 10
  • 28
1
vote
8 answers

I can't figure out how to reset a loop (see example)

I need to write a method that accepts two ints as arguments, a min and a max. On the first line i need to print all numbers in that range (inclusive). On the next line I start with min+1, print all numbers up to max, and then go back to the front of…
JP Wile
  • 156
  • 3
  • 11
1
vote
1 answer

Flatten nested for loop with dependent variables

I want to perform: for i in list_a: for j in list_b[i]: print(i, j) Is it possible to do it using itertools? I am looking for something like: for i, j in itertools.product(list_a, list_b[i]) I want to do that for speed and readability.
tupui
  • 5,738
  • 3
  • 31
  • 52
1
vote
0 answers

Copy from Excel Worksheets to specific Powerpoint slides

I am transferring data from excel to powerpoint slides with an automated script by using EXcel VBA. I'm trying to copy the usedrange of a excel worksheet and paste it to as a image in a powerpoint Template of 4th slide and from there on it should…
P. Swapna
  • 13
  • 1
  • 5
1
vote
0 answers

Add a value on a string

I wonder how do I add an additional value next to 'epigenetics' in the statement below:
Diego
  • 49
  • 1
  • 1
  • 7
1
vote
0 answers

How to speed up nested loops in R

I have 2 data.frames: users that contains longitude and latitude of every user stops that contains longitude and latitude of every bus stop I would like to calculate % of users that have at least 1 bus stop in specific radius (meters). So I…
David Lexa
  • 189
  • 2
  • 13
1
vote
3 answers

Does a nested loop advance the file pointer

I have the following text file (file.txt): 1 2 inside 3 4 outside 5 6 and when I run the following code: with open("file.txt") as f: for value in f: print("outer loop (value): ",value,end="") if "inside" in value: …
Mike
  • 504
  • 7
  • 23
1
vote
2 answers

How to parallelize this nested loop in python

I am trying to improve the performance of my code and can't figure out how to implement multiprocessing module in it. I am using linux (CentOS 7.2) and python 2.7 The code that I need to run in a parallel environment: def start_fetching(directory): …
user4516335
1
vote
2 answers

How to avoid nested 'for loops' in Python ?

I have dataframe (df) of the form : SERV_OR_IOR_ID IMP_START_TIME IMP_CLR_TIME TIME_BIN 0 -1447310116 23:59:32.873000 00:11:28.755000 1 1673545041 00:00:09.182000 00:01:06.912000 2 -743717696 …
Shreyas
  • 419
  • 1
  • 5
  • 14
1
vote
1 answer

How to increase the value of y by one every time on this loop

I am doing some basic Python in Blender and I want to add in a grid of cubes. If you can imagine that as count is 5 this creates a 5x5 grid of 25 cubes. However, I've got the code working so that the x axis increases each time but don't know how to…
Charlie
  • 230
  • 1
  • 9
1
vote
2 answers

Initialize nested Object from a list

I have a list of integers (Levels). I want to initialize a nested Object of Filter called myFilter as below(Filter is a class with two properties: Value and NextFilter): var myFilter = new Fliter{ Value = Levels[0], NextFilter = new…
Elnaz
  • 2,854
  • 3
  • 29
  • 41