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

Get rid of nested for loops with std::ranges

Let I have a code: for (auto& a : x.as) { for (auto& b : a.bs) { for (auto& c : b.cs) { for (auto& d : c.ds) { if (d.e == ..) { return ... …
w00drow
  • 468
  • 2
  • 11
5
votes
3 answers

implementing multi-level "iterator" in PHP

I'm trying to create a iterator like this one, for a list of comments: // the iterator class, pretty much the same as the one from the php docs... abstract class MyIterator implements Iterator{ public $position = 0, $list; public…
Alex
  • 66,732
  • 177
  • 439
  • 641
5
votes
3 answers

Python, Avoid ugly nested for loop

I'm new to python programming. I have tried a lot to avoid these nested for loops, but no success. My data input like: [ { "province_id": "1", "name": "HCM", "districts": [ { "district_id": "1", "name": "Thu…
5
votes
4 answers

Store result from a python nested loop into a dictionary

While doing some coding exercise, I came across this problem: "Write a function that takes in a list of dictionaries with a key and list of integers and returns a dictionary with standard deviation of each list." e.g input = [ { …
stackword_0
  • 185
  • 8
5
votes
3 answers

Is there a way to print all substrings of a string in O(n) time?

I have an input abcde. I'm trying to output something like this: a ab abc abcd abcde b bc bcd bcde c cd cde d de e I can't make a code which is without nested loops. My question is what is the solution of this problem with O(n) time complexity? …
Azmain1234
  • 77
  • 1
  • 7
5
votes
1 answer

How to make a nested for loop for cylinders around a sphere(coronavirus) in p5.js?

I'm pretty new and learning p5.js, and I am trying to make a 3D coronavirus in p5.js with a sphere and a bunch of cylinders.. You can see my sketch here: https://editor.p5js.org/zzzzzij/sketches/frE9-37R var sketch = function (p) { …
zzzzziji
  • 51
  • 1
5
votes
2 answers

Inputted integer to char array - Java

So I recently attempted to do the HackerRank challenge that involved counting a number's holes and adding them up, and after a bit of research, I ended up making it into a char array and choosing three values to increment and add up to the sum as…
Syz
  • 63
  • 4
5
votes
10 answers

access array number 2 and 3 to be in one string array

How to have output : ID: 0001 Name: Mike Birthday: London 21/05/1989 Hobby: Reading My below code is undefined, I want the array city + date to be together in the birthday. My code was not, check my code below : var input = [ …
Zr Classic
  • 303
  • 4
  • 14
5
votes
2 answers

Stream - Nested Collection - Convert to Map

Let's say i have 2 classes. Course Class public class Course { private int id; private String name; } Student Class public class Student { private int id; private String name; private List courses; } I have…
Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116
5
votes
2 answers

cant iterate nested for loop as wanted -python -maybe a simple mistake

I have tried the code below: the purpose is to generate a dictionary where each key has a list as a value. The first iteration goes well and generates the item as I want it, but the second loop, the nested for loop, doesn't generate the list as…
Pepin Peng
  • 457
  • 1
  • 8
  • 21
5
votes
2 answers

Time complexity O(N) of nested loops with if-statement: O(N^4)?

I am trying to figure out a tight bound in terms of big-O for this snippet: for(int i = 1 ; i <= n ; i++) { for(int j = 1; j <= i*i ; j++) { if (j% i == 0){ for(int k = 0 ; k
5
votes
4 answers

Javascript Nested Array Return Even Numbers

I am trying to write a function that prints only the even numbers from a nested array. Here is my attempt and it keeps returning "undefined". function printEvents(){ var nestedArr = [[1,2,3],[4,5,6],[7,8],[9,10,11,12]]; for (var i = 0;…
huisleona
  • 85
  • 1
  • 10
5
votes
2 answers

Searching specific rows in a multi-dimensional array

I'm new to java programming and I can't wrap my head around one final question in one of my assignments. We were told to create a static method that would search a 2-D array and compare the numbers of the 2-D array to an input number...so like…
Cameron
  • 51
  • 1
  • 5
5
votes
4 answers

Iteration counter for double loops

I am trying to find the formula to count every iteration of this double for loop (in python for instance): for i in range(5): for j in range(5): count = MYSTERIOUS_FORMULA print count Here the final value of count should be 25. I…
SdFRO0QS78
  • 91
  • 1
  • 2
  • 5
5
votes
2 answers

How would I speed up this program?

I am currently attempting to solve a ProjectEuler problem and I have got everything down, except the speed. I am almost certain the reason the program executes so slowly is due to the nested loops. I would love some advice on how to speed this up. I…
jzbakos
  • 285
  • 1
  • 3
  • 12