Friday, February 11, 2011

Lesson 3: Loops

Today, we begin logic.

You might remember that long, ugly string of rotations last week.  It's messy, but it worked.  It also had the distinct advantage of teaching a lesson in multiplication:  24 copies of a rotation of 15 degrees equals 360 degrees... one full circle.

Today, we'll compress those 24 discrete steps into one nice little loop.

But first, review.  Pixels, check.  Objects, check.  Had a little struggle with how many degrees are in a circle... but we got there.  Check.

a basic "for" loop in Scratch
Now to loops.  Scratch has a nice, highly visual display of loops.  It's these little "C" shaped structures that automatically wrap around whatever commands are inside the loop.  It also uses understandable terms like "forever" and "repeat".  I like that.

Logic, it turns out, is difficult and foreign to the monster.  While she understood the basic usefulness of repeating something, when she accidentally created a nested loop (which is easy to do in a drag and drop programming environment), things got funky.  It was time to step in and play debugger.  I pointed to the steps in the script as they were being executed.  Eventually she sorted this out.


144 iterations (multiplication)
Once again, math... I had never really thought of it this way, but it turns out that stacked loops are addition, whereas nested loops are multiplication.  Fortunately I saw this one coming.  After she inadvertently built a nested loop, I threw the monster a loop by asking her to create the 24 iterations she needed to do a complete 360 degree spin (yes, it was time for a little review on the number of degrees in a circle).  Sure enough, she created the first (nested) example, and the poor dizzy pear spun six times around, instead of the expected one.
24 iterations (addition)

Once she understood (with a little help by the Daddy-debugger) how nested loops really work, she recognized this as a multiplication problem.  She went for the more elegant solution of two loops of twelve iterations, but the stacked (additive) solution would have worked as well.  This simple visualization of multiplication versus addition is pretty neat.

This is right about the point in the lessons when I run out of ideas.  But it seems like the monster picks up speed.  I didn't think we'd really get much past basic loops, so we kicked in to conditionals.

Stay tuned for Lesson 3, part 2:  conditionals!

No comments:

Post a Comment