Wednesday, October 29, 2014

Computer Science Day 8 - Loops

I'm just coming off a conference (Fall CUE in Napa Valley), so I am PUMPED. UP.

We've been continuing our computer science work and have hit some pretty heavy stuff - for loops.

Code.org and Scratch both kind of avoid the complication that is a for loop; they use the repeat block to make the concept more accessible. This is all well and good, but our goal is to eventually move kids away from block-based languages and into Python, Javascript, etc. Long story short: kids need to know for loops.

...plus, you can do some pretty awesome stuff with for loops...

I searched and searched for resources out there that could help me make this content easier for students to digest, but I came up short. The most help I could find was from folks who were recommending teaching while loops instead of/before for loops; this makes total sense, since a for loop is, to some extent, just a re-worked while loop. However, kids aren't going to be SEEING while loops yet - when they use a repeat block and click "show code" on code.org, they'll see a for loop! So, I figured, why not teach for loops first?

I decided to jump right in. I began the lesson with a somewhat practical example: "If I wanted to write some sort of code that would get every student in this class to say their first name, how would I do that?"

We haven't had a ton of experience coming up with these things without some blocks to use as inspiration and hints, so kids were a bit stumped. We broke it down: we would need some way of organizing the class so that I could move in alphabetical order through everyone; we would need to have a command that would make someone say something out loud; we would have to use a variable that could hold someone's name.

Of course, in nearly every classroom, teachers organize students by number according to their last name. We were able to check that off easily. Next, we decided to get started. We came up with this pseudo-code:

student1.say(name);

I tapped my imaginary, invisible "run" button on the white board and, lo and behold, Amanda said, "Amanda!"

We celebrated.

Next, some discussion: what do we do now? I added,

student1.say(name);
student2.say(name);

In my first class, no joke, a student complained almost instantly. "This is gonna take forEVER!" I couldn't have asked for a better "Need to Know".

We stepped back (in most classes we got up to student 3 before students vocally spoke up about how long this would take) and looked for patterns (a major computational thinking step). Obviously, we noticed that the only thing that changed in each line was the number of the student.

At this point, I took the bull by the horns and said, "Class, I want you to just watch the board and study it as I write this amazing new code using what is called a for loop."

for (var stNum = 1; stNum < 32; stNum++) {
stNum.say(name);
}

Of course, students didn't get this at first. For loops are mind-boggling for the novice (and, sometimes, intermediate) since the way they are executed is so no-linear.

First, you make a variable and set it to a value (var stNum = 1). Then, you check to see that the variable meets some condition (stNum < 32). Then, you jump straight inside those curly brackets {} and execute all the code inside. After that, you go BACK to the top and do something to the for loop variable (stNum++, which means make stNum one more; ++ is an incrementor, meaning it takes whatever the current value is and makes it equal to one higher). Finally, you check the for loop condition (stNum < 32) and, if the criteria is met, you run through the curly bracket {} code again. This repeats until the for loop's condition is no longer met, at which point it jumps to the end of the curly brackets and continues with the rest of the program.

Yes, I said pretty much all of this to the class. I don't think a lot of it stuck. But you know what? I think that's kind of okay. The explanation is sound (at least I'm pretty sure it is), and I probably reached 10% of the students. Most of them won't even really need to know this if they stick with Scratch and code.org, but those who are interested will pick up on this amazing programming tool.

At the very least, when kids click "show code" after using a repeat block on code.org, I won't be bombarded with innumerable, "What's this weird line mean?" They'll just think, "Oh, it's that weird for loop thing." heh.

So, all of this was pretty much just the first 10-15 minutes of the lesson. I spent some time going through the loop with them step by step, and in one class we got TOTALLY CRAZY and re-wrote the loop to get only the even numbered students (or odd numbered) to say their name (for even, initialize stNum to equal 2, then instead of making it one bigger, make it two bigger using stNum += 2).

At this point, I figured I'd show them what they'd ACTUALLY BE USING that day by loading up Scratch to give them a little sneak peek. I brought up this simple program to show how I made the cat dance (your stereotypical first Scratch program), and used a repeat block to make him move back and forth 5 times, then 10 times, then 5000 times. Kids eat that stuff up - I kept him going throughout the entire lesson to show, "Yes, the cat is STILL dancing!"

At this point, I figured I had bamboozled them enough. We grabbed our devices and logged onto code.org to get coding. Kids got right into it - though they still made mistakes with the repeat blocks. Common errors. Like, if I have 5 "move forward" blocks, I can put a "repeat 5 times" block around them to make that happen over and over - but the bird's moving 25 times instead of 5! If I want to use a repeat block to make him move five times, I should just have one "move forward" block inside, and set the repeat number to 5. That's the topic of my next lesson...
Post a Comment