Skip to content

Nested Loops#

Pretty much all the various elements of programming languages can be combined. You can have if-statements within while-loops. You can call functions from almost anywhere. And you can put loops into loops.

When to use this#

Two-Dimensional Things#

One way of thinking about this is like going from "one-dimensional" to "two-dimensional".

Two Processing sketches, one showing a single line of circles, the other showing a grid of circles

two dimensional loop
size(200, 200);
background(40, 30, 90);
textSize(16);
textAlign(CENTER, CENTER);
for (int column = 0; column < 10; column++) {
    for (int row = 0; row < 10; row++) {
        circle(column * 20, row * 20, 15);
    }
}

Choose Wisely!

when using nested loops, it is especially important to not mix up your variables. In this case, I went from the standard i iterator/counter name to a more expressive row and column to convey what these numbers are supposed to be. I then made sure I put the column in the place of the x-coordinate and the row in the place of the y coordinate!

Sorting#

Besides two-dimensional (row/column) data like tables or pixels in an image, this can be used for conceptually very different things as well! Imagine you want to sort this hand of cards by their numeric value. How do you do that? You may just answer "I just sort them", but if you recall the Exact Instruction Challenge, I would like to go into more detail!

six random playing cards, numbers 5, 7, 6, 2, 4 and 3

Flow Chart explaining a bubble sort

So, sorting these cards by the flowchart above would work like this: On the inner loop, we're going throught the whole list of cards once. Whenever the left card is greater than the right, we just swap those two cards. The outer loop makes sure we go over that inner loop often enough so that all high cards have enough possibilities to bubble into their correct slots.

Program Output
# Outer Loop starts iteration #0
Cards: [5, 7, 6, 2, 4, 3] (currentCardIndex = 0)
  ✅ Keep cards 5 and 7 as they are
Cards: [5, 7, 6, 2, 4, 3] (currentCardIndex = 1)
  🔀 Swap cards 7 and 6!
Cards: [5, 6, 7, 2, 4, 3] (currentCardIndex = 2)
  🔀 Swap cards 7 and 2!
Cards: [5, 6, 2, 7, 4, 3] (currentCardIndex = 3)
  🔀 Swap cards 7 and 4!
Cards: [5, 6, 2, 4, 7, 3] (currentCardIndex = 4)
  🔀 Swap cards 7 and 3!
This concludes iteration #0. Cards are now: [5, 6, 2, 4, 3, 7]

# Outer Loop starts iteration #1
Cards: [5, 6, 2, 4, 3, 7] (currentCardIndex = 0)
  ✅ Keep cards 5 and 6 as they are
Cards: [5, 6, 2, 4, 3, 7] (currentCardIndex = 1)
  🔀 Swap cards 6 and 2!
Cards: [5, 2, 6, 4, 3, 7] (currentCardIndex = 2)
  🔀 Swap cards 6 and 4!
Cards: [5, 2, 4, 6, 3, 7] (currentCardIndex = 3)
  🔀 Swap cards 6 and 3!
Cards: [5, 2, 4, 3, 6, 7] (currentCardIndex = 4)
  ✅ Keep cards 6 and 7 as they are
This concludes iteration #1. Cards are now: [5, 2, 4, 3, 6, 7]

# Outer Loop starts iteration #2
Cards: [5, 2, 4, 3, 6, 7] (currentCardIndex = 0)
  🔀 Swap cards 5 and 2!
Cards: [2, 5, 4, 3, 6, 7] (currentCardIndex = 1)
  🔀 Swap cards 5 and 4!
Cards: [2, 4, 5, 3, 6, 7] (currentCardIndex = 2)
  🔀 Swap cards 5 and 3!
Cards: [2, 4, 3, 5, 6, 7] (currentCardIndex = 3)
  ✅ Keep cards 5 and 6 as they are
Cards: [2, 4, 3, 5, 6, 7] (currentCardIndex = 4)
  ✅ Keep cards 6 and 7 as they are
This concludes iteration #2. Cards are now: [2, 4, 3, 5, 6, 7]

# Outer Loop starts iteration #3
Cards: [2, 4, 3, 5, 6, 7] (currentCardIndex = 0)
  ✅ Keep cards 2 and 4 as they are
Cards: [2, 4, 3, 5, 6, 7] (currentCardIndex = 1)
  🔀 Swap cards 4 and 3!
Cards: [2, 3, 4, 5, 6, 7] (currentCardIndex = 2)
  ✅ Keep cards 4 and 5 as they are
Cards: [2, 3, 4, 5, 6, 7] (currentCardIndex = 3)
  ✅ Keep cards 5 and 6 as they are
Cards: [2, 3, 4, 5, 6, 7] (currentCardIndex = 4)
  ✅ Keep cards 6 and 7 as they are
This concludes iteration #3. Cards are now: [2, 3, 4, 5, 6, 7]

Dozens of Algorithms for Sorting!

There are dozens of sorting algorithms, and all of them ultimately arrive at the same result. With short lists like the card example above, it does not matter much which algorithm you take, but if you are sorting thousands or even millions of items, it suddenly starts to matter if your sorting algorithm suits your problem. Other sorting algorithms have names like "quick sort", "merge sort" or "bogosort".

Animated Sorting Algorithms ... sort of

There are many animations of sorting algorithms, but by far my favorite illustrations are these videos here.

Use the included stuff

It is exceedingly rare that you will need a custom sorting algorithm. We will get into sorting more once we talk about arrays, but for now I just want to say: your programming language will have some way of sorting things for you. Use that. It will be fine 99.9% of the time.

Lists of Stuff#

Another really common thing are lists. Say you have a large shopping list ahead of you:

Shopping List
# Groceries
- Vegetables
- Pasta
- Oil
- Salt
- Coffee

# Hardware Store
- Screws
- Lightbulb

# Art Supplies
- Pencils
- Acrylic Colors
- Canvas
- Brushes

They way you would get that done would look somewhat like that:

Pseudocode
for (all of the shops I need to go to) {
    for (all of the items at that shop) {
        shop that item
    }
}

Boom. Nested loops. The inner loop is concerned with the items in one shop, while the outer loop makes sure you go to all the shops.

Relevant excerpt from Learning Processing#

(the section starts at 3:04:48 and runs through 3:12:29, the video should start and stop on these automatically.)