## Wednesday, January 06, 2016

### Teaching Looping as a Concept

I’m introducing loops to my Freshmen this week. I’m trying a couple of new things. I thought I might put them out there in hopes of getting feedback but also possibly hearing how others introduce the concepts.

I’ve talked for years about the stairs. Everyone knows what happens if you don’t end your walking loop up or down stairs. We know to stop when we see that we are at the top or bottom of the stairs (while/until loops). I talk about how blind people know to count the steps in the places they regularly find themselves (for loops). It works ok.

Today I had one student count the people in the room. How did they do that? When did they know to stop? What would have happened if they double or triple counted? I need to ask how it would be different if people were moving next time. Then I had someone name three people in the room and we talked about that process some. Students haven’t thought about how they do things like this but they are basically loops.

Students seem to sometimes struggle with counters and accumulators which are commonly used with loops. Today I tried introducing them with money. Money seems to get attention. Go figure. I handed one student some coins and asked them to tell me how many coins there are. I give the same set of coins to a second student and asked them for the value of the coins. So we have a count and a total. Now we can talk about that similarities and differences in the two algorithms. It seemed to make an impact. At least everyone was paying attention.

So how do you introduce the concept of loops? What sort of real life examples do you use? Please share what works for you. I have a new class in three weeks and I want to get better.

Algot Runeman said...

One aspect of a loop is "Doing the same thing over and over" since that really isn't the case.

The iterative piece usually has an impact on the looped job. The output is usually shifted or updated from one step to the next. A loop which simply put the same output into the same place on the screen 100 times would not impress anybody. Nor would just going from the floor to the first step of a stairway accomplish much (not counting the physical exercise [cpu cycles?]).

Impressing the importance of the increment in the iteration process might be useful.

Auro said...

I also struggle to find good examples to introduce loops. Interestingly, I've been teaching a Saturday Scratch programming classes to Grade 3-7 kids, and the kids had no issue with using loops - they seemed intuitive for those kids.

I've introduced the topic by giving kids a small packet of candies (Smarties, here in Canada, M&M's elsewhere) with a flowchart that asks "Do you have any left?" From the yes side, I have them eat one and then loop back to the question. From the No side, we are done. We "follow" the flowchart to eat the candies and the kids can visually see the loop in the flowchart.

I think the comments about increment are very valid as well and I'm not sure how to incorporate this.

Algot Runeman said...

"An Empty Loop"

Imagine yourself on a merry-go-round, not a particularly fast one. You sit on the bench, not a horse. With your eyes, closed, you might only barely know you were moving. After a bunch of revolutions, you get off at the ramp you got on. Nothing accomplished.

Visualize (or better yet, create) turtle art composed of a set of circles, not drawn one on top of the others like a merry-go-round, but with a small turn before each. After a while, you'll have something that looks something like a flower.

The visual loop with it's potential to make something real and beautiful was a part of the LOGO language's goal.

The more visual the loop concept is, the easier it is to appreciate the power of it, I think.

The exploration can be done in the browser using an online site like artscript.co.uk

Garth said...

It is interesting that your examples are all While loops. Keep counting until you run out of things to count. I can get the kids to understand loops fairly easily. "Pick up the pencil. Put the pencil down. Do it 18 times. A For loop." "Take a marble out of bucket 1 and put it in bucket 2. When you run out of marbles in bucket 1, quit. A While loop." They pick up the syntax and can use For loops right away. While loops get ugly. The when to quit confuses some. They also have issues when to use which.