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
24
votes
10 answers

Find out which combinations of numbers in a set add up to a given total

I've been tasked with helping some accountants solve a common problem they have - given a list of transactions and a total deposit, which transactions are part of the deposit? For example, say I have this list of numbers: 1.00 2.50 3.75 8.00 And I…
SqlRyan
  • 33,116
  • 33
  • 114
  • 199
22
votes
6 answers

for loop vs while loop vs foreach loop PHP

1st off I'm new to PHP. I have been using for loop,while loop,foreach loop in scripts. I wonder which one is better for performance? what's the criteria to select a loop? which should be used when we loop inside another loop? the code which I'm…
Techie
  • 44,706
  • 42
  • 157
  • 243
21
votes
5 answers

How to speed up python loop

I had a look at several dicussions on several sites and none of them gave me a solution. This piece of code takes more than 5 seconds to run : for i in xrange(100000000): pass I'm working on an integer optimization problem and I have to use an …
s_xavier
  • 311
  • 1
  • 2
  • 7
20
votes
10 answers

C#: Nested conditionals vs continue statement

In using ReSharper recently, it is suggesting I reduce nesting in certain places by inverting if conditions and using the continue statements. nested conditionals: foreach(....) { if(SomeCondition) { //do some things …
KP.
  • 13,218
  • 3
  • 40
  • 60
20
votes
7 answers

Creating N nested for-loops

Is there a way to create for-loops of a form for(int i = 0; i < 9; ++i) { for(int j = 0; j < 9; ++i) { //... for(int k = 0; k < 9; ++k) { //N-th loop without knowing N at the compile time. Ideally I'm trying to figure out a way to…
Andrew
  • 347
  • 1
  • 2
  • 10
18
votes
5 answers

Pythonic shortcut for doubly nested for loops?

Consider if I had a function that took a tuple argument (x,y), where x was in the range(X), and y in the range(Y), the normal way of doing it would be: for x in range(X): for y in range(Y): function(x,y) is there a way to do for xy in…
Bolster
  • 7,460
  • 13
  • 61
  • 96
17
votes
6 answers

Simplifying / optimizing a chain of for-loops

I have a chain of for-loops that works on an original list of strings and then gradually filtering the list as it goes down the chain, e.g.: import re # Regex to check that a cap exist in string. pattern1 = re.compile(r'\d.*?[A-Z].*?[a-z]') vocab =…
alvas
  • 115,346
  • 109
  • 446
  • 738
16
votes
6 answers

PHP Looping Template Engine - From Scratch

For a group project I am trying to create a template engine for PHP for the people less experienced with the language can use tags like {name} in their HTML and the PHP will replace that tag with a predefined variable from an array. As well as…
Pez Cuckow
  • 14,048
  • 16
  • 80
  • 130
16
votes
2 answers

How to break out of nested for loops in Julia

I have tried to break out of nested loops in a quite ineffective way: BreakingPoint = false a=["R1","R2","R3"] b=["R2","R3","R4"] for i in a for j in b if i == j BreakingPoint = true println("i = $i, j = $j.") end if…
zyc
  • 427
  • 1
  • 4
  • 12
16
votes
2 answers

How to jump to next top level loop?

I have a for loop nested in another for loop. How can I make it so that when something happens in the inner loop, we exit and jump to the next iteration of the outer loop? u <- 0 for (i in 1:100) { u <- u + 1 j <- 1000 for (e in 1:30)…
user1421972
  • 161
  • 1
  • 1
  • 5
15
votes
2 answers

Nested loop in Vue

I have an object of objects that I am passing with vue and I am doing this to run:
  • @{{ question }}
but I am getting…
Victor Oliveira
  • 1,109
  • 1
  • 12
  • 29
15
votes
1 answer

Getting $index of grand parent in a nested loop

How to access the index of the grand parent in the nested loop? For example:
Suwer
  • 260
  • 3
  • 10
15
votes
2 answers

Bash exit doesn't exit

I wonder why this script continues to run even with an explicit exit command. I have two files: file1.txt with the following content: aaaaaa bbbbbb cccccc dddddd eeeeee ffffff gggggg file2.txt with the following…
Tulains Córdova
  • 2,559
  • 2
  • 20
  • 33
15
votes
5 answers

Is there a way to loop through a multidimensional array without knowing it's depth?

So far, if I have to loop through a multidimensional array, I use a foreach loop for each dimension. e.g for two dimensions foreach($array as $key=>$value) { foreach($value as $k2=>$v2) { echo } } What do I do when I don't know…
14
votes
14 answers

How to terminate outer loop in nested loops?

What is the best way to terminate all nested loops in the example below. Once the if statement is true, I want to terminate the outer for statement (with I). In the other words I need the whole loop to stop. Is there better way than setting I to…
Mirial
  • 247
  • 3
  • 8