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  
3806 questions
1908
votes
36 answers

How do I break out of nested loops in Java?

I've got a nested loop construct like this: for (Type type : types) { for (Type t : types2) { if (some condition) { // Do something and break... break; // Breaks out of the inner loop } } } Now…
boutta
  • 22,571
  • 7
  • 33
  • 45
580
votes
32 answers

How to break out of multiple loops?

Given the following code (that doesn't work): while True: #snip: print out current state while True: ok = get_input("Is this ok? (y/n)") if ok.lower() == "y": break 2 #this doesn't work :( if ok.lower() == "n": break …
Matthew Scharley
  • 115,776
  • 51
  • 189
  • 215
513
votes
16 answers

What's the best way to break from nested loops in JavaScript?

What's the best way to break from nested loops in Javascript? //Write the links to the page. for (var x = 0; x < Args.length; x++) { for (var Heading in Navigation.Headings) { for (var Item in Navigation.Headings[Heading]) { …
Gary Willoughby
  • 46,098
  • 37
  • 127
  • 193
349
votes
8 answers

Breaking out of nested loops

Is there an easier way to break out of nested loops than throwing an exception? (In Perl, you can give labels to each loop and at least continue an outer loop.) for x in range(10): for y in range(10): print x*y if x*y > 50: …
Michael Kuhn
  • 6,967
  • 6
  • 24
  • 26
333
votes
20 answers

Can I use break to exit multiple nested 'for' loops?

Is it possible to use the break function to exit several nested for loops? If so, how would you go about doing this? Can you also control how many loops the break exits?
Faken
  • 9,902
  • 16
  • 55
  • 72
224
votes
23 answers

Breaking out of a nested loop

If I have a for loop which is nested within another, how can I efficiently come out of both loops (inner and outer) in the quickest possible way? I don't want to have to use a boolean and then have to say go to another method, but rather just to…
GurdeepS
  • 58,809
  • 95
  • 236
  • 371
222
votes
15 answers

How to break nested loops in JavaScript?

I tried this: for(i = 0; i < 5; i++){ for(j = i + 1; j < 5; j++){ break(2); } alert(1); } only to get: SyntaxError: missing ; before statement So, how would I break a nested loop in JavaScript?
Mask
  • 29,767
  • 47
  • 97
  • 123
131
votes
6 answers

How can I break an outer loop with PHP?

I am looking to break an outer for/foreach loop in PHP. This can be done in ActionScript like so: top : for each(var i:MovieClip in movieClipArray) { for each(var j:String in nameArray) { if(i.name == j) break top; } } What's…
Marty
  • 37,476
  • 18
  • 87
  • 159
123
votes
6 answers

Breaking/exit nested for in vb.net

How do I get out of nested for or loop in vb.net? I tried using exit for but it jumped or breaked only one for loop only. How can I make it for the following: for each item in itemList for each item1 in itemList1 if item1.text = "bla…
KoolKabin
  • 15,407
  • 35
  • 103
  • 144
115
votes
5 answers

Single Line Nested For Loops

Wrote this function in python that transposes a matrix: def transpose(m): height = len(m) width = len(m[0]) return [ [ m[i][j] for i in range(0, height) ] for j in range(0, width) ] In the process I realized I don't fully understand how…
Asher Garland
  • 4,293
  • 4
  • 23
  • 28
110
votes
15 answers

How to break out of nested loops?

If I use a break statement, it will only break inner loop and I need to use some flag to break the outer loop. But if there are many nested loops, the code will not look good. Is there any other way to break all of the loops? (Please don't use goto…
user966379
  • 2,475
  • 2
  • 18
  • 26
84
votes
12 answers

Iterate Multi-Dimensional Array with Nested Foreach Statement

I think this might be a pretty simple question, but I haven't been able to figure it out yet. If I've got a 2-dimensional array like so: int[,] array = new int[2,3] { {1, 2, 3}, {4, 5, 6} }; What's the best way to iterate through each dimension of…
Tyler Murry
  • 2,325
  • 3
  • 24
  • 40
79
votes
2 answers

How to break outer loops from inner structures that respond break (loops/switch)

How to I break an outer loop from within an nested structure that responds to the break statement in Swift? For example: while someCondition { if someOtherCondition { switch (someValue) { case 0: // do something …
nhgrif
  • 58,130
  • 23
  • 123
  • 163
73
votes
2 answers

Looping through python regex matches

This has to be easier than what I am running into. My problem is turning a string that looks like this: ABC12DEF3G56HIJ7 into 12 * ABC 3 * DEF 56 * G 7 * HIJ And I can't, for the life of me, design a correct set of loops using REGEX matching.…
da5id
  • 789
  • 1
  • 5
  • 6
72
votes
2 answers

Breaking the nested loop

I'm having problem with nested loop. I have multiple number of posts, and each post has multiple number of images. I want to get total of 5 images from all posts. So I am using nested loop to get the images, and want to break the loop when the…
user1355300
  • 4,577
  • 18
  • 41
  • 69
1
2 3
99 100