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

Javascript - Loop doesn't work correctly while performing a simulation

I am trying to run a simulation of a library check in/out system with prototypes, objects and nested loops. I am having trouble properly integrating the pseudo code into the loop itself and would appreciate any help. //while loop or for loop for 90…
Sarah Hyland
  • 267
  • 3
  • 9
1
vote
2 answers

Alternatives to global variables: persistent variables and nested functions in MATLAB

First, I have had a look at this excellent article already. I have a MATLAB script, called sdp. I have another MATLAB script called track. I run track after sdp, as track uses some of the outputs from sdp. To run track I need to call a function…
bissi
  • 65
  • 7
1
vote
2 answers

Interesting loops and booleans

I am coding for my programing class which is supposed to be made to read and fine if in a 2 layer multidimensional array has 4 equal values in a row in either a diagonal, vertical, or horizontal fashion. Here is my code. public static boolean…
1
vote
2 answers

Concatenating two lists of Strings element wise in Python without Nested for loops

I have two lists of strings: ls1 = ['a','b','c','d'] and ls2 = ['k','j','l','m'] I want to create a 3rd list: ls3 = ['a-k','a-j','a-l','a-m','b-k','b-j','b-l','b-m'...'d-m'] which has 16 elements. I can achieve this quite easily with the…
Rohan
  • 507
  • 1
  • 4
  • 15
1
vote
1 answer

How to normalize complex nested json in python?

I am trying to normalize complex nested json in python but I am unable to parse all the objects out. I am referencing the code from this page. https://medium.com/@amirziai/flattening-json-objects-in-python-f5343c794b10 sample_object =…
xyz
  • 11
  • 4
1
vote
1 answer

Python nested loop not working as intended

I'm working on an assignment for school where I have a text file: data.txt which looks like this:(instead of 'name' there are actual names I just replaced them here) 10001-31021 'name' 2015.12.30. 524432 10001-31121 'name' 2016.03.21.…
1
vote
2 answers

How to construct in R a parallel version of nested for loop to compute values for a square matrix where the function is dependent on i and j?

I have a function that takes i and j as parameters and returns a single value and I currently have a nested loop designed to compute a value for each entry in a square matrix. But in essence since each individual value can be computed in parallel.…
Tree
  • 41
  • 1
  • 5
1
vote
1 answer

java.lang.ArrayIndexOutOfBoundsException?

I am currently teaching my self some basic Java through a text book and gave myself a "homework problem" but I'm having difficulty when writing to a .txt file of a method. It doesn't want to take more than 2 people. Here's a quick run down of what…
1
vote
2 answers

python codefights Count The Black Boxes, modeling the diagonal of a rectangle

Given the dimensions of a rectangle,(m,n), made up of unit squares, output the number of unit squares the diagonal of the rectangle touches- that includes borders and vertices. My algorithm approaches this by cycling through all the unit…
Carl
  • 41
  • 9
1
vote
1 answer

Bash 'if' statement in 'for' loop in an 'if' statement

I just can't get the code below to work. Code: #!/bin/bash ip=$1 first=$2 last=$3 if (( $first >= 1 && $first <= 255 && $last >= 1 && $last <= 255 && $first <= $last)); then range=0 else range=1 fi if [[ $ip =~…
H0rizonfire
  • 23
  • 1
  • 6
1
vote
2 answers

Web scraping every forum post (Python, Beautifulsoup)

Hello once again fellow stack'ers. Short description.. I am web scraping some data from an automotive forum using Python and saving all data into CSV files. With some help from other stackoverflow members managed to get as far as mining through all…
Norbis
  • 89
  • 2
  • 9
1
vote
4 answers

Number half Pyramid Python

I'm trying to print a half pyramid that stars on the left side in python. So far, this is my code for i in range(1,12): for j in range(12 - i): print(" ", end = " ") for j in range(1, i): print(j, end = " " ) …
fijilemon12
  • 43
  • 3
  • 11
1
vote
1 answer

Create a loop that goes through a nested list that can be appended

We're coding a password saver and I'm stuck on Nested List loops. passwords = [["yahoo","XqffoZeo"],["google","CoIushujSetu"]] if(choice == '2'): #Lookup at password print("Which website do you want to lookup the password for?") for…
Inky
  • 11
  • 2
1
vote
1 answer

nested if statements within for loops

I have a general question regarding multiply-nested statements. For "complicated nesting" (>3/4 layers), what is a better approach, especially when iterating AND using if-statements? I have a lot of files, some of which are in sub-directories,…
1
vote
2 answers

Variable levels of nested loops with inner breaks using Itertools

Experienced developer learning Python. I'm iterating through the combinations k at a time from a list of size n. I've been using from itertools import chain, combinations for subset in (combinations(range(n), k)) : …
user4894
  • 111
  • 2