I’ve been wondering lately what it is about loops (in computer programs) that is so hard for students to get their heads around. A college professor was telling me (back a while ago but it stuck with me) that they had assigned a program to print out the words to The Twelve Days of Christmas and had explicitly asked student to use loops. And when you think about it this is a natural for loops because of all the repetition. A good number (or bad depending on point of view) had actually submitted solutions without loops. Some of these students had previously taken and passed Advanced Placement Computer Science in high school! What’s up with that?
Clearly it is not lazy. using loops is the way lazy people would do it. (Though I like to think that there is a fine line between lazy and efficient at times) No, students had used cut and paste and a lot of typing to do this all inline.
Now loops are something we do without thinking all the time. Climbing stairs is a while loop. Think about it – we repeat the same step motions until we get to the top or bottom of the stairs. We check, usually with our eyes, to see when we are there and then change our motions. We’ve all seen what happens when people don’t check haven’t we? Blind people climbing familiar steps memorize the number of steps and effectively execute a for/next loop to do the same thing. Eating is the same. We keep putting food in our mouth until either we are full or we run out of food which ever comes first.
And yet all too often students fail to see how programming syntax allows them to do the same things in a program. I don’t get it.
Loops of course are all the same in programming. Oh the syntax is different for different types of loops and in different programming languages but basically they have the same components.
In the middle somewhere useful work happens – giving the benefit of the doubt.
Here is a Small Basic example
For i = 1 To 10 ' Set initial conditions
t = t + i ' pretend this is useful
EndFor ' change the value of i and see if we are done
Here is a C# while loop
TwoWord = "ABC"; // Set an initial condition
TwoWord = Console.ReadLine(); // Change the condition
while (TwoWord.CompareTo("exit") == 1); // See if the ending condition is met
I don't remember loops being hard, but I do remember having a hard time with addresses and dereferencing them. And pass-by-value and pass-by-reference. I remember when the light went on, it's just like the address on the front of a house. But I'd always have to work through that when trying to understand code that used addresses. Now it's of course second-nature. But loops weren't hard for me, or arrays.
I think that arrays and loops are one of those things that tend to be an early indicator of if people are really going to "get" programming. We probably need to find better ways to help more people understand and us (grok if you will) loops and arrays so that they can progress on.
Thank goodness you did not post the answer to the Twelve Days of Christmas - I use that as one of my programs on the first semester final. In the directions I tell the students to use loops( min of two) and not put just lots and lots of couts. And you know that I will still get about 6 or 7 a class that will hand write out all twelve versus. And for some reason this year a lot of my students want to use do/while loops to count? When I ask them why? most just say they forgot the for works better. - just stepped away to help a student in tutorials and guess what he used to count 20 times... do/while. As the old songs says - and the beat goes on..
Back in Jr High School, I had to write some sentence 100 times as punishment for some terrible thing that I had done (no, I don't remember what it was).
I asked if I could do it on a computer and the teacher said that would be OK. Of course I wrote a program with a for-loop to do this -- I even had a few if-then-else's to introduce typos to make it look more legit.
Perhaps that is the assignment we should be giving all students when we want them to learn loops : Feign outrage at something the class does and punish them all by having them write some sentence 100 times. This punishment should coincide with them learning about loops.
Actually, thinking about this more, it may need to be "a million times" to prevent them from simply cut-n-pasting. But allow the students to turn in either the sentences OR a program to generate them.