While loops gone wild
I hope that my students know that I too feel programming is hard. In fact, I hope some of them realize that programming is exciting and meaningful to me in large part because it’s such a challenge. When students complain about difficulty, I like to remind them that they’re in school to stretch themselves but sadly that
doesn’t tend to resonate much with them. Go figure.
In Creative Computing, we recently spent time working with indefinite loops. I think most introductory programming instructors would be shocked and appalled at how long we’ve spent primarily focusing on one
measly construct. Indeed, while loops are a half-hour lecture in most college-level intro to programming courses. But here’s the deal – while loops are hard. What does it take to use a while loop properly? I’ve tried to think of all the little things necessary to know how to interpret and create code using indefinite loops but I’m sure I’ve forgotten bits and pieces! I poked around a little for formal articles with these sorts of breakdowns for various programming constructs but wasn’t very successful. Pointers?
Understanding while loops requires…
- syntactic knowledge
- start the statement with while, have some condition in parentheses, end the line with a colon then indent things that need to be repeated
- valid boolean operators (assignment = vs. equality ==)
- creating complex boolean expressions with and and or (for example, my students often write things like value == 2 or 3 which won’t work as expected in Python)
- how to group statements into a block
- understanding of scope rules
- if I use a variable in my test, it has to have been initialized
- if I create a variable in my loop, it won’t be available later
- understanding of flow of control
- if I make a method call in my loop body, that method is run then Python keeps executing statements after the call
- if my condition stops being true somewhere in the loop body, that doesn’t make me magically jump out
- the test is executed every time around the loop
- converting between stopping conditions and continuing conditions
- DeMorgan’s Law is hard
I have a lot of sympathy for students who take more time to synthesize all of this and I think it’s too bad that we don’t have more best practices on breaking concepts down into digestible pieces. At the same time, I think CS courses are exciting for high-achieving students BECAUSE we haven’t figured out how to break things down into bite-sized chunks, yet! Certainly, when I was starting my perpetual quest to learn to program, I found something deeply satisfying about looking at several examples of a construct to establish a model of how it works. For students who are not used to considering an idea from different angles in an attempt to really understand it, it’s a deeply frustrating process.
I feel like this points to a deep need for differentiated instruction to keep my top end students engaged without overwhelming my bottom end. I’ve been trying to be more careful about giving students sample code that address sub-concepts one at a time and having different milestones so students at various levels can be successful. Again, not really finding best practices but that’s probably because computing education is primarily done at the college level where it’s not much of a concern for instructors (or I don’t know where to look?). Raymond Lister and John Leaney’s First Year Programming: Let All the Flowers Bloom does present some nice work supporting the idea of shifting my goals for weaker students to reading and interpreting code rather than producing entirely novel programs.
Of course, I don’t pretend to know what the right solution is. One thing I think is underscored by the overwhelming list of concepts needed to understand the humble while loop is the importance of being very deliberate about the order topics are introduced in. Sometimes I look at introductory programming course syllabi and I feel like concepts are just being thrown haphazardly into a pile instead of aiming to construct knowledge. Strong students will probably be fine but what about normal people?!