# Looping Lessons!

My 7th and 8th grade computer science students are done now with their programming unit and on to final projects!  In their programming unit, I focused hard on one particular standard in the CSTA standards:

CPP:L2:5  Implement problem solutions using a programming language, including: looping behavior, conditional statements, logic, expressions, variables, and functions.

I really think my students grew a ton in all of these areas this year. Looping was the last of these I taught. This is the sequence of lessons I used.

1) Khan Academy looping lessons:  I asked them to watch the video lesson, take the quiz, and do the “Loopy Ruler” challenge.

2) Review of While Loops and Matching challenge. We did a group discussion reviewing the structure of the loop – the initialization, the boolean expression, the iteration, and the behavior of variables. We talked about infinite loops and then I paired the kids up, cut up this set of cards, and had them match programs to outputs. We had a whole-group discussion afterward about why they made the matches they did, and any matches that were difficult to make.

3) Geometric pattern activity. Students created a program that made a drawing that used loops to make it repetitive, similar to this one.

4) Loop warmup.  Students got a partner and were asked to write code for 3 loops:  One that would run forever, one that would run exactly 5 times, and a third that would never run.  I had students share their answers on the board and discuss why they worked (or didn’t).

5) While loop quiz.  This was a really, really difficult assignment and I ended up having to abort it early.  I think it’s a great thinking activity, though, and I want to figure out how to make it work better. The prompt is this:

The line(x1,y1,x2,y2); command draws a line from x1,y1 to x2,y2. For example, line(50,70,100,130); draws a line from (50,70) to (100,130).

For this task, you will use a while loop to draw a staircase on the canvas. You should have four variables: x, y, w, and h. x,y specify the starting position of the first step. w, h specify the width and height of the stairs. Your program should have ONLY two line() commands and use a while loop to repeat them to draw the staircase.

Example:
var x = 0;
var y = 0;
var w = 70;
var h = 50;

This should draw a staircase that starts at (0,0) and each step is 70 units wide and 50 units tall.

A correct implementation looks like this:
STAIRSTEP PROGRAM

And I had a couple of students that nailed it right away, and 85% of the class that struggled, and struggled, and struggled. Some made things that looked like steps but had no loops. Others did not use the variables. Many hard-coded a single step and then did not know what to do.  For me, the math on the variables is a really straightforward thing, but for the students, this was such a weird representation of slope and addition that it just really blew their minds. I ended up helping a lot.

6) Credit Card simulation.  I introduced “for” loops with this document and program on how credit cards work.

Presentation: forloops_pdf

Credit Card simulation

I love the credit card activity for the conversation it brings about with the students. They really aren’t taught what a credit card is or what the term APR means or what you’re getting into when you swipe your card. The scenario of making payments faithfully every month, and still ending up deeply in debt, is really common and I think the simulation is powerful. I feel like I’m still looking for great prompts / tasks around this scenario to make it the activity it really could be. I would so love feedback on that and may write about it separately!

7) Debate on “for” loops vs. “while” loops. Why should we use one over the other?  Which do you prefer?  Funny how passionate students get over this topic. 🙂