Sunday, February 13, 2011

Lesson 3 part 2: the IF statement

The Monster had more momentum than I had planned for in Lesson 3, so I decided if we're doing loops, we might as well do IF.

The IF statement is in many ways the core of computer programming.  If you understand the IF statement, you understand how conditional branching is such an essential element of computing.  So I was nervous digging into IF, and I'm pretty sure that The Monster will be reviewing this a few times.

In Scratch, an IF statement looks like this:

The hexagon is where the conditional is placed, via drag and drop.  While this made sense to me, it was tricky to figure out what could go inside the hexagon and what couldn't.  There's no clear indication of "conditionals" in Scratch's color coded selection of action types.  The clearest ones we'd found, the ones that The Monster had already used, were things like keypresses.  But these were going to blow by way to fast when executing a script.  I was quickly getting into trouble.  We hadn't even used a variable yet!

The bottom line:  this was a mistake.  I should have been doing variables before I considered jumping in to the IF statement.  For us to get anything out of IF, we had to know variables, and variables are way out in front of where The Monster currently sits.

So I made a judgment call.  I decided, let's roll with this momentum, and just show The Monster how to do something cool.  I took over the mouse and, explaining everything I was doing, created a main update loop (a "forever" loop in Scratch-speak), created a locally scoped variable to store keyboard input as a directional state, and then put the variable into a series of IF statements.  It went pretty fast, and I'm pretty darn confident that it sailed right past The Monster, even though she claimed to be understanding it every step of the way.

Here are some of the ridiculous list of concepts that came into play:

  • A main update loop
  • Variables
  • TRUE and FALSE as logical concepts
  • Conditional branching
  • Grouped statements (inside an IF statement's brackets)
  • Flow of control (what happens if no statement matches an IF?)
  • X vs Y coordinate space
  • Negative numbers (!!)
  • Moving in relative space (relative to the sprite) as opposed to absolute space (X/Y)
Do I think The Monster took all that in?  No chance.  But I did let her put in quite a few of the parameters, and to see how they affected the interactive outcome.  I suspect that we'll spend the next several weeks deconstructing and reconstructing this script.  I think next would probably have to be variables...

Meanwhile, here it is, in its glory:

A simple but functional interactive prototype

No comments:

Post a Comment