# 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".

```
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!

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.

```
# 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:

```
# 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:

```
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.)