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

Breaking out of a nested for loop without using break

My project is finally complete, but my only problem is that my teacher does not accept "breaks" in our code. Can someone help me resolve this issue, I have been working on it for days and I just can't seem to get the program to work without using…
Alex
  • 79
  • 4
6
votes
5 answers

Alternative to Nested Loop For Comparison

I'm currently writing a program that needs to compare each file in an ArrayList of variable size. Right now, the way I'm doing this is through a nested code loop: if(tempList.size()>1){ for(int i=0;i<=tempList.size()-1;i++) …
KGVT
  • 98
  • 1
  • 2
  • 11
6
votes
2 answers

Dynamic nested for loops to be solved with recursion

I'm trying to get a result looking something like this: Miniors | Boys | 54kg - 62kg where every value delimited by a pipe | comes from an array containing a certain "type of restriction". For example: ageGroups, genders, weightClasses (as seen…
aup
  • 800
  • 7
  • 19
6
votes
5 answers

Iterating 2D array with pointer expressions in C

I'm practicing pointers and want to substitute pointer operations in place of the arrays to traverse through the elements of the array. I have read numerous articles and cannot grasp this concept. Can someone explain? Here I made a 2D array and…
ryan
  • 625
  • 3
  • 10
  • 23
6
votes
3 answers

Time complexity for dependant nested for loop?

Can you explain me how to find time complexity for this? sum=0; for(k=1;k<=n;k*=2) for(j=1;j<=k;j++) sum++; So, i know the outer loop has time complexity of O(logn), but since the iterations of the inner loop depends on the value of the…
user2228135
  • 239
  • 6
  • 17
6
votes
2 answers

Big O Theory- triple nested loop

If I have a function of: for(i=0;i
Danny2036
  • 93
  • 5
6
votes
4 answers

Why should I avoid using Java Label Statements?

Everywhere across the internet people say that you should avoid using label statements in java. However, I find them very useful in some cases, namely nested loops. I cannot find satisfactory answers as to why not to use them. I think that…
user230331
6
votes
3 answers

Java Big O notation of 3 nested loops of log(n)

What would the Big O notation be for the following nested loops? for (int i = n; i > 0; i = i / 2){ for (int j = n; j > 0; j = j / 2){ for (int k = n; k > 0; k = k / 2){ count++; } } …
Kailua Bum
  • 1,368
  • 7
  • 25
  • 40
6
votes
3 answers

Alternative to nesting for loops in Python

I've read that one of the key beliefs of Python is that flat > nested. However, if I have several variables counting up, what is the alternative to multiple for loops? My code is for counting grid sums and goes as follows: def horizontal(): for…
davenz
  • 399
  • 2
  • 3
  • 9
6
votes
10 answers

Pyramid of numbers in Java

I am trying to print a pyramid in Java that looks something like this: 9 8 9 8 7 8 9 8 7 6 7 8 9 8 7 6 5 6 7 8 9 8 7 6 5 4 5 6 7 8 9 8 7 6 5 4 3 4 5 6 7 8 9 8 7 6 5 4 3 2 3 4 5…
Ajit
  • 667
  • 2
  • 14
  • 27
6
votes
3 answers

Which table is considered 'inner' in a nested loop join

Can anyone tell me which table is considered to be the inner one in a nested loop join? For example if the query is from a inner join b on..., which one, a, or b will be considered inner? I knew that it is b, but from the article at dbsophic, the…
SexyBeast
  • 7,913
  • 28
  • 108
  • 196
6
votes
4 answers

combinations: avoiding multiple nested foreach

When you need to check/have combinations of array elements, how can you avoid nesting foreach? Example code: $as = array($optionA1, $optionA2) $bs = array($optionB1, $optionB2) $cs = array($optionC1, $optionC2) foreach ($as as $a) { foreach…
koen
  • 13,349
  • 10
  • 46
  • 51
5
votes
2 answers

Make nested loops more efficient?

I'm analyzing large sets of data using the following script: M <- c_alignment c_check <- function(x){ if (x == c_1) { 1 }else{ 0 } } both_c_check <- function(x){ if (x[res_1] == c_1 && x[res_2] == c_1) { 1 …
Michael LeVine
  • 197
  • 1
  • 9
5
votes
4 answers

My iteration is taking forever. Looking for a better way

Given a set of sixteen letters and an English dictionary file, need to find a solution where those sixteen letters can fit into a 4x4 grid so that a valid word can be read across each row, and down each column. My current solution: 1) Get a list of…
Slightly A.
  • 2,795
  • 2
  • 16
  • 10
5
votes
3 answers

Nested Loop Optimisation in Python for a list of 50K items

I have a csv file with roughly 50K rows of search engine queries. Some of the search queries are the same, just in a different word order, for example "query A this is " and "this is query A". I've tested using fuzzywuzzy's token_sort_ratio function…