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

Return from a nested loop in Common Lisp

I'm trying to convert this Python code into Common Lisp: for a in xrange(1,1000): for b in xrange(a,1000): c = (a**2 + b**2) ** 0.5 s = a + b + c if s == 1000: return a * b * c My first attempt was: (loop for…
Mirzhan Irkegulov
  • 17,660
  • 12
  • 105
  • 166
5
votes
7 answers

How can I handle nested looping without nesting foreach statements in Perl?

I have a Perl script which nests foreach loops as seen below. It takes a long time: #! /usr/bin/perl use strict; use warnings; my @sites = ('a', 'b', 'c'); my @servers = ('A', 'B'); my @data_type = ("X", "Y", "Z"); foreach my $site (@sites) { …
Space
  • 7,049
  • 6
  • 49
  • 68
5
votes
1 answer

Ember.js: How to view data from Ember.Map

I would like to view some contact information (name, email,...) grouped by Department but I am unable to achieve it with Handlebars. Department X Contact x1 info Contact x2 info ... Department Y Contact y1 info Contact y2 info ... I don't…
erbihanka
  • 185
  • 1
  • 10
5
votes
4 answers

Remove Strings from one Array if present in another

This is a rather fundamental question but im looking for an optimal solution.I have 2 javascript String arrays. Lets say A: ["Stark", "Targaryen", "Lannister", "Baratheon"] B: ["Greyjoy", "Tyrell", "Stark"] Since "Stark" is repeated, i want to…
karan
  • 77
  • 4
4
votes
4 answers

C pointer arithmetic snippet

I have a program that I'm trying to decode. It is translated to C from another language (whose name is not spoken here), and as I want to understand how it works, I am slowly rewriting the code and simplifying it to use all the nice logical…
Chris Lutz
  • 73,191
  • 16
  • 130
  • 183
4
votes
2 answers

efficient python function to get value of specific key in nested dict without an external lib and without knowing concret static path to key in dict

The following initial situation: I am looking for a custom function that will extract a corresponding value from a nested dict and return it without external libs and without kowning the whole static path to the corresponding key. The function…
4
votes
3 answers

Pasting values from a vector to a new column in a for loop with nested data

I have a dataframe that currently looks like this: subjectID Trial 1 3 1 3 1 3 1 4 1 4 1 5 1 5 1 5 2 1 2 1 2 3 2 3 2 3 2 5 2 5 2 6 3 1 Etc., where trial number is nested under subject ID. I need to make a…
Paul
  • 83
  • 6
4
votes
2 answers

Variable Number of Nested While Loops with Index Increment Limits Based on External Logic

In Javascript, I am attempting to iterate through an array of indices indexArray. This array can be any length, but for ease of explanation, I will use length 3 indexArray.length = 3. I have an external function: getResult(indexArray) which returns…
4
votes
2 answers

Nested For-Loop Explanation

Code written in java I'm trying to print the user's input amount of "*" on the same line. Here is what I have: if (((input / 2) + 1) == ir) { for (int ij = 1 ; ij <= input; ij++) { System.out.print("*"); } } the if statement is…
pjrahal
  • 51
  • 4
4
votes
2 answers

What are good techniques for rewriting nested for loops in Haskell?

I'm learning Haskell and currently trying to rewrite this code: case class Coord(x: Int, y: Int) def buildBoard(coords: [Coord]): String = { var str = "" for (i <- 0 to 30) { for (j <- 0 to 40) { if (coords.exists(c => c.x…
4
votes
1 answer

Python: Fetching nested dictionary values GCP Recommendations

I am trying to illicit certain key values out of an API JSON output from GCP Recommendations API using Python, and am newer to using Python. Most of the values that I am trying to illicit I can fetch without an issue, however, when I try to illicit…
4
votes
1 answer

Nested for loop in 'lists' and 'iterators'

I have these two lists l1 = [1,2,3] l2 = [10,20,30] I use this nested for loop to access all possible pairs: for a in l1: for b in l2: print(a,b) I get the following expected result: 1 10 1 20 1 30 2 10 2 20 2 30 3 10 3 20 3 30 But if…
Mahsa
  • 581
  • 1
  • 9
  • 28
4
votes
1 answer

Multiple texts output according to multiple values in 2 columns

I am starting to code in python (Pandas library) because of my work. Here is an example with pokemons but my real dataframe is more complex. I dont know if it's possible to do it in Pandas. My dataframe is like: Pokemon_Master Pokemon 1 …
4
votes
3 answers

Add all paired numbers in an array JavaScript

I have an array like this: [20,40,60,60,20] The requirement: sum any two number, if the result is divisible by 60 then count 1. Therefore this array should return 3 pairs. (20,40), (40,20), (60,60). Here is the code I write but it gave me 4…
marigi
  • 41
  • 2