<p>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:</p>

<pre><code>i = 0
while (i &lt; 100) {
  # Some code here
  i++
}
</code></pre>

<p>Then I'd ask them to simplify it to something more like:</p>

<pre><code>for (i=0; i &lt; 100; i++) {
  # Some code here
}
</code></pre>

<p>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:</p>

<pre><code>while (true) {
  # Some code here
  break if some_boolean
}
</code></pre>

<p>And try to get them to correct it to</p>

<pre><code>do
  # Some code here
while (!some_boolean)
</code></pre>

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

CC BY-SA 4.0
2020-01-27T06:29:38.147Z
Actually, it's often a n+1/2  loop,  like `while (true) { action1 ; if (some reason)  break; action 2; }. Removing the break leads to code duplication (call to action 1), or  more complicated code (extra if, or boolean indicator)
Michel Billaud
CC BY-SA 3.0
2017-06-10T14:02:42.607Z
2017-06-10T14:02:42.607Z
thesecretmaster
<p>I teach them while loops, for loops, and for each loops; in that order.</p>

<p>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.</p>

<p>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. </p>

<p>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.</p>

<p>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.</p>

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

<p>Most students settle on for loops when it's their choice.</p>

CC BY-SA 3.0
2017-06-10T14:33:24.730Z
2017-06-10T14:33:24.730Z
Ryan Nutt
<p>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.</p>

<p>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.</p>

<p>Concepts first and syntax later.</p>

CC BY-SA 3.0
2017-06-22T13:51:07.970Z
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!
Ben I.
CC BY-SA 3.0
2017-06-22T11:02:08.990Z
2017-06-22T11:02:08.990Z
Alfred Thompson
<p>Use the <code>for</code> when you know how many times to execute it, such as counting, or iterating over an array. Use <code>while</code> or <code>do .. while</code> if you don't. The choice between <code>while</code> and <code>do .. while</code> is if you need to do the loop one time before the test use <code>do ... while</code> otherwise use <code>while</code>.</p>

<p>A full explanation can be found on Stack Overflow in <a href="https://stackoverflow.com/a/2950945/7412956">this</a> accepted answer.</p>

CC BY-SA 3.0
2017-10-03T15:17:49.003Z
@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.
Michel Billaud
CC BY-SA 3.0
2017-10-03T15:24:24.473Z
@MichelBillaud When written, the programmer may not know the number of iterations, but it is still bounded by the string length at run time.
Gypsy Spellweaver
CC BY-SA 3.0
2017-10-04T09:29:32.913Z
Well, most calculations given as exercices have a well-known worst case  time-complexity, and thus the number of iterations is bounded.
Michel Billaud
CC BY-SA 3.0
2017-06-10T13:35:33.473Z
2017-06-10T13:35:33.473Z
Gypsy Spellweaver
<p>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.</p>

<p>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 <code>while true</code> loop with a break statement at the end could be better expressed as a do while loop. </p>

<p>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?</p>

CC BY-SA 3.0
2017-06-11T12:28:42.490Z
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)
Sean Houlihane
CC BY-SA 3.0
2017-06-11T23:39:57.053Z
@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.
Robert Cartaino
<lesson-ideas><looping>
How to teach which loop should be used
# How to teach which loop should be used

(Asked by: username_0 on 2017-06-10)

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?

**Tags:** lesson-ideas, looping

> **Comments:**
> **username_1**: 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)  
> **username_2**: @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.  

---

## 4 answers

### Answer by username_8 on 2017-06-10. Score: 8 | Accepted answer

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](https://stackoverflow.com/a/2950945/7412956) accepted answer.

> **Comments:**
> **username_4**: @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.  
> **username_8**: @MichelBillaud When written, the programmer may not know the number of iterations, but it is still bounded by the string length at run time.  
> **username_4**: Well, most calculations given as exercices have a well-known worst case  time-complexity, and thus the number of iterations is bounded.  

---

### Answer by username_5 on 2017-06-10. Score: 7

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.


---

### Answer by username_3 on 2017-06-10. Score: 5

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.

> **Comments:**
> **username_4**: Actually, it's often a n+1/2  loop,  like `while (true) { action1 ; if (some reason)  break; action 2; }. Removing the break leads to code duplication (call to action 1), or  more complicated code (extra if, or boolean indicator)  

---

### Answer by username_6 on 2017-06-22. Score: 4

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.

> **Comments:**
> **username_7**: 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!  

---

