## Tuesday, February 25, 2014

### Which Comes First–Loops or Arrays?

One of the classing problems in teaching a first course in computer programming is what order to teach various concepts. So many things are interdependent on each other that sometimes you wish you should teach everything at once. That would generally cause student heads to explode so its not a great option.  One area I struggle with is loops and arrays. They are both so much more interesting to me with the other. So I asked people on Facebook about this.

Most said they taught loops first and then arrays. This makes some good sense. (Even if it is how I do it.) Arrays are more useful and interesting when paired with loops after all. They are almost pointless without loops. Almost being a relative term of course. Also loops follow on directly from decision structures. Once you talk about Boolean expressions for if statements adding the idea of using them in a loop is pretty natural. Once can come up with some fun loop demos even without arrays.

And then others added opinions. Some did suggest arrays first. Once you have an array you pretty much need loops. This tends to feel like some “just in time learning” in that now you have a need so “let’s talk about a solution.

A couple of people talk about arrays as part of a larger conversation about variables. This also makes sense. Arrays are after all variables. It’s a simple step from “Student0, Student1, …” to Student[i]. This is similar to the logic behind following decision statements with loop statements in that you are taking a concept one step further.

A case can be made for any of these options. I don’t know if  there is research on which order gets the best results though. Anyone out there know of research? Most people just do what feels right for them. Or teach the same way their learned.

What really bothers me, and for what I have no clear solution, is that it feels like we talk about variables four times. Once for “normal” variables for standard data types. Later for arrays. Still later for strings. By that I mean at some point we talk about how strings are fairly special and have attributes of single item data types and also attributes similar to arrays. Lastly we talk about user defined variables and data types. Is it too much to talk about all of that at once? Probably. I can almost see the eyes glazing over.

All of this is why I think the first programming course is harder to teach than a lot of people think it is. Once you are through it a lot of things come easier. Of course once you’ve learned all the key concepts in a first programming course it all seems easy and obvious to you. Well if you learned them well they do. It is sometimes hard to remember how hard some of these concepts were the first time one heard them. It’s a new way of thinking for most people. And let’s not forget the syntax struggles!

Still I find teaching the first course rewording. I love seeing a student when something “clicks” for the first time and they see the potential. If only I could teach all the concepts at the same time.

Dan Bowen said...

Interesting Debate and changed my thinking....cheers.

Dan

Unknown said...

Interesting...I teach variables first and then foray into Strings soon after. Then I teach decision structures and soon after we do loops, at which point, we do looping with Strings. From here, jumping to arrays seems quite easy. Just a different spin on the same content!

Garth said...

Teaching this Python course has changed how I approach loops and arrays. Previously it was loops then later arrays. In Python the For loops assume the counter is an array (a list actually but same-same) so lists and For loops have to be taught at the same time. The kids immediately start an understanding of putting elements in a list. I like it.

Mike Zamansky said...

We start with scheme so it's variables -> functions -> conditionals -> recursion -> lists. It's a different approach entirely.

When I start with a Java type language, I do loops first.

As Garth said, Python is different. The for loop is foreach, and arrays are lists etc. When in Python I usually do strings -> string indexing -> slices -> list stuff and then loops

Mark Guzdial said...

Pictures come first, and you need loops to deal with all the pixels. By the way, pictures are arrays.

Alfred C Thompson II said...

Mike, recursion was very hard for me to learn. And I never used it in years of professional development. The first issue is in how I was taught and I understand it a lot better now than I used to.

I don't like it as a way of doing repetition but for those things that really require recursion it is like magic.

Alfred C Thompson II said...

Mark, I need to get a bit more comfortable with pictures myself before I use them for loops in the very early states. I do think they're great though and want to get there.