6
$\begingroup$

In most languages, there are many different types of loops. The most common ones seem to be for loop, while loops, and do while loops.

What is the best way to illustrate the differences between the loops, not in terms of syntax, but in terms of recommended usage. For example, a while loop with an iterator variables can usually be replaced by a for loop. A while true loop with a break statement at the end could be better expressed as a do while loop.

How can I get my students to use the correct loops in each situation? How can I show them when a loop can be better expressed in another form?

$\endgroup$
  • 1
    $\begingroup$ You've asked 2 questions. One is mapping a problem to the most appropriate loop form (which is simple and definitive), the other is how to teach this... I'd suggest changing the title if you meant the former (and that I think is a clearer question to ask, hoping for some teaching input as a side-product) $\endgroup$ – Sean Houlihane Jun 11 '17 at 12:28
  • 1
    $\begingroup$ @SeanHoulihane Unfortunately, the former (how to program) is not on topic. The focus of this site is educators discussing teaching methodologies, but this question (and the top answer) has an air of "when to use which loop?" rather than how to teach it. The lower-scored answers are more on task, but I was concerned that this thread took the direction it did. $\endgroup$ – Robert Cartaino Jun 11 '17 at 23:39
8
$\begingroup$

Use the for when you know how many times to execute it, such as counting, or iterating over an array. Use while or do .. while if you don't. The choice between while and do .. while is if you need to do the loop one time before the test use do ... while otherwise use while.

A full explanation can be found on Stack Overflow in this accepted answer.

$\endgroup$
  • 1
    $\begingroup$ @GypsySpelweaver Traversal of strings is also a typical use of the for loop in C. for(char *p = s; *p != '\0', p++) { do_something_with(*p); }. Doesn't fit with the "know how many times" idea, which is a remain of the for loop in Pascal or Basic, or do loop in Fortran. $\endgroup$ – Michel Billaud Oct 3 '17 at 15:17
  • $\begingroup$ @MichelBillaud When written, the programmer may not know the number of iterations, but it is still bounded by the string length at run time. $\endgroup$ – Gypsy Spellweaver Oct 3 '17 at 15:24
  • $\begingroup$ Well, most calculations given as exercices have a well-known worst case time-complexity, and thus the number of iterations is bounded. $\endgroup$ – Michel Billaud Oct 4 '17 at 9:29
7
$\begingroup$

I teach them while loops, for loops, and for each loops; in that order.

What I've found is that while loops tend to make the most sense for students. The idea of while I'm hungry, eat pizza is something they understand.

But, forgetting to change the condition is really common so we wind up with a lot of infinite loops. I sell for loops as a loop where is harder to forget to change the counter.

I tell them for each loops are a less codey way of iterating, but generally only work if you want to start at the beginning at look at every element.

As long as the code works, the only time I care what kind of loop they use is when we first introduce that type. During the while loop labs they have to use while loops. Once we've covered loops, it's their call.

I don't explicitly cover do while loops as a topic, but they do show up in a couple of project shells.

Most students settle on for loops when it's their choice.

$\endgroup$
5
$\begingroup$

I find that a useful way of teaching the different types of loops is simply giving examples and asking student to express them more concisely. For example, I'd give my students a loops of the format:

i = 0
while (i < 100) {
  # Some code here
  i++
}

Then I'd ask them to simplify it to something more like:

for (i=0; i < 100; i++) {
  # Some code here
}

For while loops, you can also teach the difference between while and do while with examples, for example you can show your students an example of the format:

while (true) {
  # Some code here
  break if some_boolean
}

And try to get them to correct it to

do
  # Some code here
while (!some_boolean)

As a bonus, this also teaches how to clean up code, which is another valuable skill.

$\endgroup$
4
$\begingroup$

I introduce loops by showing them loops in the physical world. For example I have a student take 7 steps - counting loop. An other student walks to a specified point - while/until loop. We talk about how these loops work in our minds before we talk about how they work in code.

Many daily activities can be discussed in terms of loops and we discuss several. Eating is a good "loop" with several possible indicators of when to stop. When you are full or when you are out of food or when you don't have more time to eat (end of lunch bell rang.) Steps can be counted or one can tell that they are at the top. Even better what happens if you "stop" early or late.

Concepts first and syntax later.

$\endgroup$
  • $\begingroup$ I just had a why didn't I think of that??? moment. I love any activity that gets the kids out of their chairs. Wonderful! $\endgroup$ – Ben I. Jun 22 '17 at 13:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.