Friday, October 25, 2019

A Loop By Any Other Name

My first programming language was FORTRAN IV. As I recall we had Do loops. They worked pretty much the same as what we call For loops in most languages today. We didn’t have while loops but we did the same sorts of things with line numbers, if statements, and the powerful but “evil” Go To statement. As time went on I learned more ways of specifying looping constructs including recursion. Recursion took me some time to wrap my head around. Possibly because I used some languages that didn’t even support it back in the day.

These days there are all sorts of iteration loops of various complexity and power. For a software developer that is wonderful. For a teacher? Well, it means you have to make decisions about what to teach. For a one semester, first programming course I like to keep things simple. A C-family For loop has all sorts of possibilities from the simple to some pretty complex structures. Eventually students will learn many of them but I could probably spend half a quarter just on For loops if I tried to get everything possible in and understood. The cognitive load on some possibilities feels like it would be too much for many students.

Mostly I try to focus on the base concepts. A loop has a set up piece. Variables are set to a known starting state. A loop has some sort of comparison it see if it is finished. A counter it checked, a flag is checked, or maybe an interrupt happens. But something has to stop the loop sometime. usually but we can get into those times when an infinite look is useful later. And of course something has to happen that changes the values or states that the loop is checking.

Understanding these concepts will, I sure hope, prepare students for what ever syntax or iteration style they run into over time.

When we teach students for a career rather than just for a job the concepts are much more important than the programming language or the IDE. Concepts – what a concept!

6 comments:

Unknown said...

When teaching loops, I usually begin with "SET, TEST, CHANGE" and talk about the role of the loop control variable. Usually we have to set (initialize) it, test it for continuing or stopping ( depending on the loop and finally being sure that it changes to avoid an infinite loop.

Netkonnexion said...

Of course it is better to teach concepts - it gives the student a wider scope for understanding the context of something. However, I teach computing to secondary students. I try to give them as wide a context as possible in all aspects of the curriculum. But when it comes to programming, I rarely find any comments in their notes that show they have taken on board the wider context. Perhaps it is better with university students, but at secondary level the ideas they have seem to shrink away from taking on board the wider ideas that relate to a concept. For example, we teach Python. But where I give examples of how other languages do things, the students never write those references or ideas down. They seem to only want to know how Python works and nothing else.

Alfred C Thompson II said...

I find that a lot of students have a narrow view. They look to solve only the problem in from of them and don't seem that interest is seeing reusable patterns. They also tend not to put diverse concepts together. I sometimes think that if I didn't make such a big thing of loops and arrays (or other iterative structures) they wouldn't necessarily put the two together. I think it may be related to how much we teach so many things in "silos" that are devoid of interaction with other concepts.

Mike Zamansky said...

I think it's worth noting that teachers, depending on language, can approach loops in two very different ways:

1.. Counting loops - while of C/Java for - where the programmer manually controls some variable's value until it reaches an exit condition

and

2. a "foreach" loop that iterates over some list type structure


The former is probably more natural when teaching a language like Java and the latter maybe in a Python based class.


While they can frequently accomplish the same things, they are conceptually very different:

result = ""
for i in range(len(s)):
result = result + s[i].uppper()

vs.

result = ""
foreach i in s:
result = result + i.upper()

Both loop turn build a string that is the upper case of the original but the former is much more "low level" indexing into the String structure while the former operating at a higher level.

Just some food for additional thought.

Unknown said...

Mike, one thing those examples suggest is idiom in programming languages. I remember the old joke that a good FORTRAN programmer can write a good FORTRAN program in any language. That attitude prevents people from getting the most out of a new programming language.

Garth said...

Since I know several languages poorly I cannot go wrong!