Comments on: While loops gone wild http://helenemartin.com/2010-05-while-loops-gone-wild/?utm_source=rss&utm_medium=rss&utm_campaign=while-loops-gone-wild You don't understand something until you've taught a teenager to teach a computer to do it. Mon, 08 Apr 2013 04:47:39 +0000 hourly 1 http://wordpress.org/?v=3.6.1 By: Hélène Martin http://helenemartin.com/2010-05-while-loops-gone-wild/comment-page-1/#comment-120 Hélène Martin Sat, 15 May 2010 15:34:45 +0000 http://www.helenemartin.com/?p=297#comment-120 Ben:

Very good point. Sometimes I forget that some apparent misconceptions are really just symptoms of deeper ones. It’s very hard for some students to understand that computers can only do one thing at a time since first of all we multitask all the time and secondly it feels like computers are constantly doing many things. I have students work with Scribbler robots in Python and it seems like that’s when some of them recognize the limitations of having just one instruction done at a time — “what do you mean I can’t have it play a song and move at the same time?!” Other students got the idea long ago and are not surprised at all.

I disagree that curly braces are a particular advantage in this case — I think it makes just as much sense to say that a block is an indented chunk of code than to say that it’s delineated by curly braces. That being said, it took me a while to accept that!

]]>
By: Ben Chun http://helenemartin.com/2010-05-while-loops-gone-wild/comment-page-1/#comment-119 Ben Chun Sat, 15 May 2010 06:32:58 +0000 http://www.helenemartin.com/?p=297#comment-119 Another underlying set of concepts that students need to know in order to form a successful mental model of loops and conditionals:

* The processor can only execute one instruction at a time.
* There is one and only one program counter.

I don’t explicitly introduce “program counter” as vocabulary, but the concepts are essential. Without this understanding, jumping out of the middle of a loop seems plausible. This is one situation where the extra punctuation required by the curly-bracket languages is helpful in reinforcing the concept that a loop controls an entire block of code.

]]>
By: Ryan http://helenemartin.com/2010-05-while-loops-gone-wild/comment-page-1/#comment-110 Ryan Thu, 13 May 2010 00:28:05 +0000 http://www.helenemartin.com/?p=297#comment-110 I just had a thought… I know, stop the presses, right?

As an example of why you need to declare the accumulator before the loop, consider a sum accumulator vs a product accumulator:


def sum(lst):
  for val in lst:
     acc += val
    return acc

def prod(lst):
    for val in lst:
        acc *= val
    return acc

So, in braindead languages like PHP (<3 PHP!), you could get away with the sum implementation with just a warning. Maybe it's "obvious" that acc should start out at zero. However, there's no language that I know of where the prod implementation would work: you'd always get "0" out at the end.

The fix is easy, and it illustrates the importance of defining accumulators beforehand:

def sum(lst):
    acc = 0
    for val in lst:
        acc += val
    return acc

def prod(lst):
    acc = 1
    for val in lst:
        acc *= val
    return acc

Well, that’s my idea.

]]>
By: Interesting Links 10 May 2010 http://helenemartin.com/2010-05-while-loops-gone-wild/comment-page-1/#comment-103 Interesting Links 10 May 2010 Mon, 10 May 2010 11:07:11 +0000 http://www.helenemartin.com/?p=297#comment-103 [...] us that while loops are more complicated than we sometimes remember  with a post titled While loops gone wild. She talks about some of the things she does to teach indeterminate loops and what works or not at [...]

]]>
By: Hélène Martin http://helenemartin.com/2010-05-while-loops-gone-wild/comment-page-1/#comment-101 Hélène Martin Sun, 09 May 2010 17:29:58 +0000 http://www.helenemartin.com/?p=297#comment-101 Ahh, yes, you’re correct. My Java students suffer from loop confusion as well and I kind of lumped it all together. In Python, the issue is that we often need variables as accumulators, like i in the example you give, and students for whatever reason don’t see that those need to be initialized before. We get a lot of ‘referenced before assignment’ issues that really throw students off.

Unfortunately, I don’t think there’s any one ‘best way,’ but if I find it I’ll let you know!! =)

]]>
By: Ryan http://helenemartin.com/2010-05-while-loops-gone-wild/comment-page-1/#comment-99 Ryan Sun, 09 May 2010 08:44:09 +0000 http://www.helenemartin.com/?p=297#comment-99 I’ve had trouble explaining loops to people as well. Certainly let me know if you figure out the best way to do it.

I noticed one error in your statements: “if I create a variable in my loop, it won’t be available later”

In python and PHP, among others, that is not true; both have only one local scope. Ie, this is valid in python

i = 0
while i < 5:
  i += 1
  a = i
print a, i

]]>
By: Hélène Martin http://helenemartin.com/2010-05-while-loops-gone-wild/comment-page-1/#comment-95 Hélène Martin Fri, 07 May 2010 03:00:46 +0000 http://www.helenemartin.com/?p=297#comment-95 L, I think that’s a great idea. Appealing to students’ basic understanding of the world helps a lot. But it’s certainly not enough — students rarely have problems with the IDEA of a while loop… it’s the implementation that baffles them.

Notice that in your first sentence you suggest doing exercises on repetition UNTIL something happens. This does appeal to our intuition very nicely, but it’s not how while loops work, which is exactly part of the problem! Scratch has repeat until, but Python and most other standard languages only have a “while a certain condition holds true” construct. If we have students think in terms of repeating until, which is quite natural, then we run into DeMorgan’s Law when coding. Heck, that trips me up sometimes, too!

I do think some kinesthetic activities can help but unfortunately I don’t think they’re the full solution. But what is, right?

]]>
By: L bricker http://helenemartin.com/2010-05-while-loops-gone-wild/comment-page-1/#comment-94 L bricker Fri, 07 May 2010 02:52:47 +0000 http://www.helenemartin.com/?p=297#comment-94 I think one thing you could do is teach the concept of looping outside of programming first, doing some exercises on repetition UNTIL something happens. That will give them the idea of the “testing”

A good example is just walking.

While I am not hitting a wall
walk forward one step.

The test is “I am not hitting a wall” (you can discuss boolean logic here, and play with it in terms of or a corner, etc). So you have a lot of ideas there that you can bring in

With the body of hte loop, you could change the requirements so that each time you are stepping one extra step. How do you keep track of that.

Maybe have a piece of paper/counter on your body as an internal variable, but instead have a student keep track of the counter for a variable outside the context of the “you” loop.

]]>