<h1>Your pupils are already be doing nested loops.</h1>

<pre><code>foreach week {
   foreach weekday{
      wake up
      clean teeth
      eat breakfast
      goto school
      foreach period{
         goto class
         do lesson
      }
      go home
      do evening stuff
   }
   do saterday stuff
   do sunday stuff
}
</code></pre>

<h1>What about some formulaic music.</h1>

<p>Music has a lot of repetition, could you get them to play some music based on a nested loop algorithm. I am thinking drumming on tables, and a lot of shouting.</p>

<p>e.g. something like</p>

<ul>
<li>Repeat 4 times:

<ul>
<li>Teacher says: “say yo 3 times”.</li>
<li>Teacher says: “clap 4 times”</li>
<li>Teacher says: “…”</li>
</ul></li>
</ul>

<h1>Sorting algorithms — dance, or just moving about.</h1>

<p>Get pupils to act out sorting algorithms. And identify the loops.
Here is how to do it in the form of dance <a href="http://youtube.com/watch?v=lyZQPjUT5B4" rel="nofollow noreferrer">http://youtube.com/watch?v=lyZQPjUT5B4</a> </p>

CC BY-SA 3.0
2017-10-03T21:14:43.890Z
Shouldn't "go home" be outside the "foreach period" loop?  I'm sure the students would prefer how you have it now, though.
jwodder
CC BY-SA 3.0
2017-10-04T07:57:18.717Z
@Jwodder You can use errors like that as a teaching opportunity. But I have now fixed it.
ctrl-alt-delor
CC BY-SA 3.0
2017-10-06T17:53:54.820Z
Perfect timing. We're doing nested loops on Monday.
Ryan Nutt
CC BY-SA 3.0
2017-10-03T16:00:28.940Z
2017-10-04T07:56:16.143Z
2017-10-04T07:56:16.143Z
ctrl-alt-delor
<p>Well, you may want to do this outside. You may want to have a lot of time, and for some, you might want to have medical personnel available. </p>

<p>To really do it as an active learning exercise, you need three student monitors, one for each loop, who will keep count and signal each iteration and the end. </p>

<p>First suppose that the <code>&lt;doSomething&gt;</code> action is the same in all cases and is simple and fast, say <em>clap hands</em>. The three cases will have a different number of overall claps, of course, and the last will take a while. </p>

<p>Next, suppose that the <code>&lt;doSomething&gt;</code> actions can be different for each occurrence (I'll still assume they don't depend on the current value of the iterator.) We might have something like the following:</p>

<p><strong>Fully Un-nested</strong></p>

<pre><code>for (int i = 0; i &lt; 10; i++) {
            clapHandsOnce();
}
for (int j = 0; j &lt; 10; j++) {
            shout("hooray");
}
for (int k = 0; k &lt; 10; k++) {
            standUpSpinAroundSitDown();
}
</code></pre>

<p><strong>Partially Nested</strong></p>

<pre><code>for (int i = 0; i &lt; 10; i++) {
    for (int j = 0; j &lt; 10; j++) {
        clapHandsOnce();
    }
    for (int k = 0; k &lt; 10; k++) {
        shout("hooray");
    }
}
</code></pre>

<p><strong>Fully Nested</strong></p>

<pre><code>for (int i = 0; i &lt; 10; i++) {
    for (int j = 0; j &lt; 10; j++) {
        for (int k = 0; k &lt; 10; k++) {
            clapHandsOnce();
        }
    }
}
</code></pre>

<p>Well, in the last case, hands are pretty sore by now, but the need for medical comes in if you try: </p>

<pre><code>for (int i = 0; i &lt; 10; i++) {
    for (int j = 0; j &lt; 10; j++) {
        for (int k = 0; k &lt; 10; k++) {
            clapHandsOnce();
            shout("hooray");
            standUpSpinAroundSitDown();
        }
    }
}
</code></pre>

<p>Before you try it, I'd suggest you consider setting the upper limit of each loop a bit lower. </p>

<p>But still, the iteration values aren't used. So...</p>

<pre><code>for (int i = 0; i &lt; 10; i++) {
    for (int j = 0; j &lt; 10; j++) {
        for (int k = 0; k &lt; 10; k++) {
            shout("" + i + j + k);
        }
    }
}
</code></pre>

<p>or</p>

<pre><code>for (int i = 0; i &lt; 10; i++) {
    for (int j = 0; j &lt; 10; j++) {
        for (int k = 0; k &lt; 10; k++) {
            shout("" + 100*i + 10*j + k);
        }
    }
}
</code></pre>

<p>Night falls. Parents file lawsuits....</p>

<hr>

<p>There is actually a version that might be doable, and more informative as well. Consider: </p>

<pre><code>for (int i = 0; i &lt; 10; i++) {
    standUpSpinAroundSitDown();
    for (int j = 0; j &lt; 10; j++) {
        shout("hooray");
        for (int k = 0; k &lt; 10; k++) {
            clapHandsOnce();
        }
    }
}
</code></pre>

<p>Your monitors will have a bit more to do here, perhaps, choreographing their parts, but the interleaving is instructive as to what is going on. </p>

CC BY-SA 3.0
2017-10-03T17:24:08.967Z
2017-10-03T18:51:43.330Z
2017-10-03T18:51:43.330Z
Buffy
<p>I have been rewriting my unit on nested for loops for AP Computer Science (thus, we are using Java).  I would like to use some sort of physical activity to drive home the differences between roughly these three ideas:</p>

<p><strong>Fully Un-nested</strong></p>

<pre><code>for (int i = 0; i &lt; 10; i++) {
            // do something
}
for (int j = 0; j &lt; 10; j++) {
            // do something
}
for (int k = 0; k &lt; 10; k++) {
            // do something
}
</code></pre>

<p><strong>Partially Nested</strong></p>

<pre><code>for (int i = 0; i &lt; 10; i++) {
    for (int j = 0; j &lt; 10; j++) {
        // do something
    }
    for (int k = 0; k &lt; 10; k++) {
        // do something
    }
}
</code></pre>

<p><strong>Fully Nested</strong></p>

<pre><code>for (int i = 0; i &lt; 10; i++) {
    for (int j = 0; j &lt; 10; j++) {
        for (int k = 0; k &lt; 10; k++) {
            // do something
        }
    }
}
</code></pre>

<p>How can I get kids out of their seats for this one?</p>

<looping><active-learning>
Using physical activity to explain the nested for loop
# Using physical activity to explain the nested for loop

(Asked by: username_0 on 2017-10-03)

I have been rewriting my unit on nested for loops for AP Computer Science (thus, we are using Java).  I would like to use some sort of physical activity to drive home the differences between roughly these three ideas:
Fully Un-nested
for (int i = 0; i < 10; i++) {
            // do something
}
for (int j = 0; j < 10; j++) {
            // do something
}
for (int k = 0; k < 10; k++) {
            // do something
}

Partially Nested
for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 10; j++) {
        // do something
    }
    for (int k = 0; k < 10; k++) {
        // do something
    }
}

Fully Nested
for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 10; j++) {
        for (int k = 0; k < 10; k++) {
            // do something
        }
    }
}

How can I get kids out of their seats for this one?

**Tags:** looping, active-learning


---

## 2 answers

### Answer by username_1 on 2017-10-03. Score: 6

Your pupils are already be doing nested loops.
foreach week {
   foreach weekday{
      wake up
      clean teeth
      eat breakfast
      goto school
      foreach period{
         goto class
         do lesson
      }
      go home
      do evening stuff
   }
   do saterday stuff
   do sunday stuff
}

What about some formulaic music.
Music has a lot of repetition, could you get them to play some music based on a nested loop algorithm. I am thinking drumming on tables, and a lot of shouting.
e.g. something like

Repeat 4 times:


Teacher says: “say yo 3 times”.
Teacher says: “clap 4 times”
Teacher says: “…”


Sorting algorithms — dance, or just moving about.
Get pupils to act out sorting algorithms. And identify the loops.
Here is how to do it in the form of dance http://youtube.com/watch?v=lyZQPjUT5B4

> **Comments:**
> **username_2**: Shouldn't "go home" be outside the "foreach period" loop?  I'm sure the students would prefer how you have it now, though.  
> **username_1**: @Jwodder You can use errors like that as a teaching opportunity. But I have now fixed it.  
> **username_3**: Perfect timing. We're doing nested loops on Monday.  

---

### Answer by username_4 on 2017-10-03. Score: 4

Well, you may want to do this outside. You may want to have a lot of time, and for some, you might want to have medical personnel available. 
To really do it as an active learning exercise, you need three student monitors, one for each loop, who will keep count and signal each iteration and the end. 
First suppose that the  action is the same in all cases and is simple and fast, say clap hands. The three cases will have a different number of overall claps, of course, and the last will take a while. 
Next, suppose that the  actions can be different for each occurrence (I'll still assume they don't depend on the current value of the iterator.) We might have something like the following:
Fully Un-nested
for (int i = 0; i < 10; i++) {
            clapHandsOnce();
}
for (int j = 0; j < 10; j++) {
            shout("hooray");
}
for (int k = 0; k < 10; k++) {
            standUpSpinAroundSitDown();
}

Partially Nested
for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 10; j++) {
        clapHandsOnce();
    }
    for (int k = 0; k < 10; k++) {
        shout("hooray");
    }
}

Fully Nested
for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 10; j++) {
        for (int k = 0; k < 10; k++) {
            clapHandsOnce();
        }
    }
}

Well, in the last case, hands are pretty sore by now, but the need for medical comes in if you try: 
for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 10; j++) {
        for (int k = 0; k < 10; k++) {
            clapHandsOnce();
            shout("hooray");
            standUpSpinAroundSitDown();
        }
    }
}

Before you try it, I'd suggest you consider setting the upper limit of each loop a bit lower. 
But still, the iteration values aren't used. So...
for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 10; j++) {
        for (int k = 0; k < 10; k++) {
            shout("" + i + j + k);
        }
    }
}

or
for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 10; j++) {
        for (int k = 0; k < 10; k++) {
            shout("" + 100*i + 10*j + k);
        }
    }
}

Night falls. Parents file lawsuits....

There is actually a version that might be doable, and more informative as well. Consider: 
for (int i = 0; i < 10; i++) {
    standUpSpinAroundSitDown();
    for (int j = 0; j < 10; j++) {
        shout("hooray");
        for (int k = 0; k < 10; k++) {
            clapHandsOnce();
        }
    }
}

Your monitors will have a bit more to do here, perhaps, choreographing their parts, but the interleaving is instructive as to what is going on.


---

