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

Nested loop pattern return null

I am trying to make id for items. But after A6 B6.., it set to null. The minimum number of item is 0 and max is 36. The pattern should be A1-A6 B1-B6 C1-C6 D1-D6 E1-E6 F1-F6 For example if I have 32 items. The id should stop at F2. My…
user5405692
1
vote
1 answer

Complexity of nested for loops starting from the last iteration

function solution(A) { var len = A.length; cnt = 0; for(i = 0; i < len - 1; i++){ for (a = i + 1; a < len; a++){ if (A[i] == A[a]){cnt++;} else{continue;} } if(cnt > 1000000000){return…
1
vote
2 answers

Nested for loop - batch Script

Hello Batch File experts, I wrote this piece of code which will print the Latest file version present in the folder in comparison to file name sent as argument, however these line seems to work accordingly when I remove the outer for loop, which I…
1
vote
2 answers

angular filter is returning an object. How to avoid that?

I have a JSON file and it's format is as shown below. Some values are just strings, while others are objects with key value pairs. [{ "question": "How many names do you have?", "answer": "1) Mark, 2) Ram, 3) Sam, 6) Paul, 7) Bob" …
Abhi
  • 13
  • 2
1
vote
1 answer

nested while-loop that doesn't match the condition

I am trying to make a program that print the following numbers : 1 1 1 2 1 3 1 4 2 1 2 2 2 3 2 4 The code is public class JavaApplication8 { public static void main(String[] args) { int i = 1; int j = 1; while (i…
Smas
  • 33
  • 7
1
vote
1 answer

Add CheckBoxes on runtime in a square for loop in C#

I want to dynamically add Checkboxes in a x*y matrix. The simplest way that came to my mind to start a for loop which goes by O(n²). I have 2 TextBoxes which are for the width and height of the matrix. In my example i did 10x10; When i press the…
m0ddixx
  • 13
  • 2
1
vote
3 answers

Nested Loop Running time complexity analysis

I got a insertion sort algorithm below, it includes the nested for loop: public InsertionSort( AnyType [] a ){ int m; for( int n = 1; n < a.length; n++ ){ AnyType temp = a[n]; for( m = n; m > 0 && tmp.compareTo( a[ m - 1] )…
1
vote
1 answer

Stuck in Indenting spaces according to numbers using a nested loop

I need some help. I want my program to print different numbers according to the user's input and indent that number with that number of spaces as well. Must be using a nested loop. Expected Output: (lets say user types 3) 0 1 2 3 for…
Anonymous
  • 489
  • 1
  • 7
  • 18
1
vote
0 answers

Defining an `n` length nested loop in VBA

Building a portfolio asset allocator, for assets A, B, C, D, and want to expand it to n assets. With four assets, can run four nested loops. The issue is to create n nested loops, and then apply the constraint, detailed below, and the middle nest,…
rrg
  • 655
  • 2
  • 6
  • 24
1
vote
2 answers

Breaking out of nested loops with user input

Is there an efficient way to break out of a nested loop by having a user simply providing an input at the particular time they want to break out (e.g. press a key)? I have conditional statements in the loops themselves that I can use to break the…
Mathews24
  • 681
  • 10
  • 30
1
vote
1 answer

How to write nested for loops in methodes (clean code)

Clean code means for me: only one task for each methode and no nested loops. When I got the following code, I asked myself, how can I avoid nested for loops and encapsulate them in methods. private String getUser(){ for (FieldConfigScheme…
locibin
  • 13
  • 3
1
vote
1 answer

time-complexity of nested loop

Why is the time-complexity of function (n) { //this loop executes n times for( i = 1 ; i <= n ; i + + ) //this loop executes j times with j increase by the rate of i for( j = 1 ; j <= n ; j+ = i ) print( “*” ) ; } Its running…
Ankita
  • 37
  • 8
1
vote
2 answers

C++ templates to make several version of function with different constant

Can I use template to create several instantiations of some function, different only in some constant parameter? The number of alternatives for this parameter is fixed. E.g. I want not to rewrite (where upper is in 1..32 in powers of…
osgx
  • 90,338
  • 53
  • 357
  • 513
1
vote
1 answer

Handlebars.js How to itarate through nested array using outer loop index

Hi. My JSON is: variants : [ { name : "one", segments : [ { value : 1 }, { value : 2 }, { value : 3 } ] }, { name : "two", segments : [ { value : 4 }, { value : 5 }, { value : 6 } ] …
1
vote
3 answers

Addition Table: Python3+

For the question below I done what I can shown in , but don't really know where to go from there. I just started working with end values and am probably going to destroy this small code farther. # Inputs range_start = int(input("Enter start…
1 2 3
99
100