3
$\begingroup$

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?

$\endgroup$
6
$\begingroup$

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

$\endgroup$
  • 2
    $\begingroup$ Shouldn't "go home" be outside the "foreach period" loop? I'm sure the students would prefer how you have it now, though. $\endgroup$ – jwodder Oct 3 '17 at 21:14
  • $\begingroup$ @Jwodder You can use errors like that as a teaching opportunity. But I have now fixed it. $\endgroup$ – ctrl-alt-delor Oct 4 '17 at 7:57
  • $\begingroup$ Perfect timing. We're doing nested loops on Monday. $\endgroup$ – Ryan Nutt Oct 6 '17 at 17:53
4
$\begingroup$

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 <doSomething> 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 <doSomething> 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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.