<p>Maybe you could use physical objects to help students visualize how to utilize these loops.</p>

<p>For example for the bounded loops: grab 5 apples, label them with some number, and show how you go through each apple (e.g., bite each of them one at a time).</p>

<p>For the simple unbounded, try this: put a set of brand new apples on the table, ask if there are any of them that haven't been bitten yet. If so, then bite one, then ask again and bite another non-bitten apples until all of them are bitten.</p>

CC BY-SA 3.0
2018-01-30T10:02:00.060Z
Some good ideas, however I am looking for advice on order to teach loops in a non-ideal language.
ctrl-alt-delor
CC BY-SA 3.0
2018-01-29T20:43:14.823Z
2018-01-30T10:00:20.587Z
2018-01-30T10:00:20.587Z
frostshoxx
<p>While your ideal order makes sense, you really should try to plan the course based on the limitations of the language that you're working with. In this case, instead of thinking about the different conceptual loops, you should think about the different loop syntaxes that the language provides.</p>

<p>In this case, the order in <a href="https://cseducators.stackexchange.com/a/4271/58">@Bryan R's answer</a> (if/else -> while -> arrays -> for) makes the most sense to me. Then, once you've finished teaching all the different sytaxes for loops, you can talk about how some loops are equivalent, for example:</p>

<pre><code>a = 2
i = 0
while i &lt; 5 do
  a = a*2
  i = i+1
end
# Ends with a == 64
</code></pre>

<p>is the same as</p>

<pre><code>a = 2
for (i = 0; i &lt; 5; i++) {
  a = a*2;
}
# Ends with a == 64
</code></pre>

<p>(also, see <a href="https://cseducators.stackexchange.com/a/2634/58">my answer to a different loop-related question</a> about how for loops and while loops are really the same thing)</p>

<p>and then show them your ideal loops, because those are a far better way to classify different types of loops than based on syntax alone, but they won't be able to see the importance of that classification until they are comfortable with the syntax for the different loops.</p>

CC BY-SA 3.0
2018-01-29T14:29:13.383Z
2018-01-29T14:29:13.383Z
thesecretmaster
<p>I prefer to teach <code>while</code> loops as part of my unit on <code>if</code>/<code>else</code> statements since the syntax and thought process are virtually identical.</p>

<p>I then introduce <code>for</code> loops when teaching lists (in Python) or arrays (in Java).  With <code>for</code> loops, language impacts which style you likely use first.  With Python, I use the <code>for item in list</code> structure first.  In Java, I use the C style <code>(for i=0; i&lt;...)</code> loop first.  </p>

<p>I don't try to introduce all styles of looping at once as I prefer to develop the concepts across units.</p>

CC BY-SA 3.0
2018-01-30T09:38:33.740Z
I've seen `for`-loops introduced after seing a lot of examples of `while`-loops with a counter that is incremented once per loop, and seing the utility of a more compact way to do that.
Arthur
CC BY-SA 3.0
2018-01-30T10:13:38.950Z
Makes sense. I was thinking that with an ideal language I would teach iteration before conditional. Conditional code is over used, in most programs that I have seen. I have also done a proof that you can do any thing (Turing complete) without conditionals, so long as you can loop. (also you only need 2 loops: the super loop and the container iterator). Try it for a challenge.
ctrl-alt-delor
CC BY-SA 3.0
2018-01-29T12:28:29.710Z
2018-01-29T14:00:35.087Z
2018-01-29T14:00:35.087Z
Bryan R
<p>All of the IF and Loop structures are based on two fundamental operations: Test and Branch. The only difference between an IF-Else and a Loop of any kind is whether you branch downward or back to the top. On a flow chart this just knocks you in the nose: the only difference is the arrowhead on one line.</p>

<p>When something so varied and complex can be reduced to such simplicity, you know that it is a fundamental idea. Teach that idea first, and then show how it is implemented in all of the various structures as you introduce each one. Now your students understand that programming is not magic, it is not about memorizing incantations and syntax: it is about simple, logical analysis of a problem, exactly how people and animals have been breaking down problem solutions in to basic steps all along. There is nothing new or surprising here, and it need not be confusing.</p>

<p>With this understanding, your students can absorb new structures and languages themselves, knowing that <strong>it has to be simple underneath</strong>, because circuits are simple, and people are not really able to do vastly complex things without breaking them down in to steps. Test. Branch. Done.</p>

CC BY-SA 3.0
2018-01-29T15:33:06.253Z
This is a very interesting answer. Welcome to [cseducators.se]! My only suggestion is that it might be helpful for you to draw a more direct connection between the question and the answer.
thesecretmaster
CC BY-SA 3.0
2018-01-29T15:54:27.007Z
I really like this perspective, and I hadn't quite thought about it stated so simply and cleanly before.  (I also don't generally teach absolute beginners, so I don't have a ton of experience having to clarify this particular idea.)  This also puts a nice punctuation mark on Bryan R.'s [observation](https://cseducators.stackexchange.com/a/4271/27), "I prefer to teach `while` loops as part of my unit on `if`/`else` statements since the syntax and thought process are virtually identical."
Ben I.
CC BY-SA 3.0
2018-01-29T16:09:36.330Z
While I agree that this is how they are usually implemented. This is back to the structured/un-structured high/low level argument, that you were not here for. (yes I said usually: for example on the ARM — the most popular ≥ 32bit cpu on the planet at this time — conditional and iteration are implemented different at the very low level. For a backward conditional branch of a loop the branch predictor assumes that the branch will happen. For a forward conditional of an if it is assumed not to happen. Arm also has conditional instructions. )  But thanks for contributing.
ctrl-alt-delor
CC BY-SA 3.0
2018-01-29T16:45:12.863Z
"*The only difference between an IF-Else and a Loop of any kind is whether you branch downward or back to the top*" just isn't true.  DO...WHILE and LOOP...UNTIL and FOR X UNTIL Y STEP Z are nothing but (very useful) syntactic sugar laid over TEST and BRANCH.
RonJohn
CC BY-SA 3.0
2018-01-29T17:09:31.883Z
@RonJohn What you said is exactly what I got out of what user4226 posted. He also said that both if/else and all of the loop constructs are just test and branch.  What are you saying that is different?
Ben I.
CC BY-SA 3.0
2018-01-29T19:17:07.513Z
@BenI. I directly quoted this part that isn't true.  Specifically, "**whether you branch downward or back to the top**"  Then I explained why.  Maybe I didn't do a good enough job, though: you can have loops that check the condition at the top (WHILE...DO) and you can have loops that check the condition at the bottom (DO...WHILE).  However, C and it's derivatives don't have DO...WHILE, so it seems to have fallen out of favor.  I agree with everything else that user4226 (and Bryan R) wrote.
RonJohn
CC BY-SA 3.0
2018-01-29T19:39:27.803Z
@RonJohn Oh, I see.  The quibble was about whether the jump upwards was as a result of the test, or whether it was an unconditional jump.  It still seems to reduce to the same basic idea: loops involve a jump backwards while `if`s involve a jump forwards.
Ben I.
CC BY-SA 3.0
2018-01-29T20:36:42.033Z
@BenI. Correct, that's my disagreement.  Here's what I'm talking about:  https://en.wikipedia.org/wiki/Do_while_loop and https://en.wikipedia.org/wiki/While_loop
RonJohn
CC BY-SA 3.0
2018-01-30T10:45:50.280Z
@RonJohn Hm, C most certainly has a DO...WHILE construct. Did you mean that Python does not have it?
pipe
CC BY-SA 3.0
2018-01-30T14:30:31.423Z
@pipe you're absolutely right.  It's been a while since I've used C, and forgot about it.
RonJohn
CC BY-SA 3.0
2018-01-30T15:31:34.910Z
The importance of this answer is that it emphasizes underlying concepts. I think it can *help*, not only hurt, to teach this first - *un*structured control in various forms, and then teach structured control. That presents an excellent opportunity to teach **why** structured control is preferable. IOW, more or less follow the history -- the history itself evolved the way it did for good reasons. It's not just about teaching the use of a particular language; it's about teaching what's going on - *why* we do things the way we do. The various iteration constructs did not fall from the sky.
Drew
CC BY-SA 3.0
2018-02-07T10:13:37.567Z
We do iterations, not for the pleasure to do so, but as a way to compute some results (say average of an array). Unstructured control is what the computer can do, at its lower level. But what we do goes from "divide sum by size" then "compute sum by adding all values" then "use a variable with initial value 0 and add each values", and "range over all array elements" and "use an index starting from 0 etc" (if we dont have a foreach loop over collections) and LAST use a for/while/if-goto loop. We jump from the sky.
Michel Billaud
CC BY-SA 3.0
2018-01-29T15:25:56.507Z
2018-01-29T15:25:56.507Z
user4226
<p>Perhaps you are creating a problem where none exists. You seem to be assuming that looping needs to be covered all at once and all together, rather than distributed over some some longer range interspersed with other topics. </p>

<p>The two fundamentally different kinds of looping are <em>definite</em> in which the program knows how many iterations are necessary prior to the loop and <em>indefinite</em> in which it does not. Those two concepts can be taught back to back so that the essential concepts are there. The others can wait a bit. Looping over a range or list can wait until you teach collections, for example. Infinite looping can be taught as either definite or indefinite (while True:...) depending on your language.</p>

<p>I certainly would  <em>not</em> choose a language based solely on its direct syntactic support for all of the possible loop forms. Most of them are excluded, in fact, to keep the number of keywords in a language to a reasonable number. Once the language is Turing Complete things can be built using idioms of the language and a student needs to become familiar with that idea (idiomatic programming) in any case. </p>

<p>In any case, looping, like everything else should be taught in a Spiral framework in which ideas are introduced correctly but incompletely early on and then returned to, reinforced and expanded later on. This is useful when teaching programming since languages are themselves recursive and so difficult to teach strictly linearly. But a Spiral pedagogical technique is also essential due to the way the mind works and the way people learn - via reinforcement and repetition. </p>

<p>Therefore simple ideas of looping can come early and more complex ones later. The level of explanation changes, since the early, simple, explanations fertilize the ground for the later ones, both expanding and deepening the knowledge. <em>Spiral</em> is one of the fundamental <a href="https://smile.amazon.com/Pedagogical-Patterns-Educators-Joseph-Bergin/dp/1479171824" rel="nofollow noreferrer">Pedagogical Patterns</a>. </p>

CC BY-SA 3.0
2018-01-29T13:10:36.907Z
You are correct that we should not do it all at once. While I have not mentioned, the other thinks that could be interleaved, please do not take this of evidence that I would not interleave other learning.
ctrl-alt-delor
CC BY-SA 3.0
2018-01-29T13:17:03.063Z
I have added a section to question to explain the problem.
ctrl-alt-delor
CC BY-SA 3.0
2018-01-29T12:20:32.290Z
2018-01-29T14:43:52.590Z
2018-01-29T14:43:52.590Z
Buffy
<blockquote>
  <p>For an infinite loop we use <code>while True</code> in python, however students ask or struggle with this. I can not explain it without explaining while condition. So pupils have to hold something that they do not understand in their head.</p>
</blockquote>

<p>Could you restructure this to something that makes sense to your students in plain English?</p>

<p>Something like:</p>

<pre><code>programShouldRun = True

while programShouldRun:
    doStuff()
</code></pre>

<p>Call your boolean variable whatever makes sense in the context you're working in, whether that is <code>programShouldRun</code>, <code>answerNotFound</code>, <code>fileStillHasLines</code>, etc.</p>

CC BY-SA 3.0
2018-01-30T12:25:12.820Z
Hmmm. If `programShouldRun` is a variable then this isn't specifically an infinite loop. It may turn out to be, but that depends on the future possible value of the variable. Constants are possible in Python, but a deeper concept and language specific, of course.
Buffy
CC BY-SA 3.0
2018-01-30T13:27:09.620Z
Yeah, that's definitely possible, though on the other hand that could be used as a lead-in to the difference between an infinite loop and one that eventually terminates. 

"Up until now we've said that `programShouldRun` is always `True`, but what do you think happens if we set it to `False` at the bottom of the loop?" 

Alternatively: "So, this program runs forever, but we want to exit when [thing we are looking for]. Does anyone have any idea about how we'd go about fixing that?"
Petter Brodin
CC BY-SA 3.0
2018-01-30T13:49:38.143Z
But don't miss the point that the question was about _classifying_ loops. An infinite loop should clearly say, semantically or syntactically, that "this loop is infinite". Controlling it specifically with a variable says just the opposite. It is about the meaning of the code, not just about how it plays out. Students (and professionals) often write infinite loops by mistake, forgetting to change a condition in some way. Your technique works only in a language that can clearly specify a constant value. For example an alternate symbol for True.
Buffy
CC BY-SA 3.0
2018-01-30T12:12:23.983Z
2018-01-30T12:12:23.983Z
Petter Brodin
<p>I only ever taught my fellow students but this method was the one that everyone seemed to get.</p>

<p>There's a track, and there's a runner, he's running forever. When you type that <code>while</code> in, you're telling him to do something <em>while</em> he runs. Then you put a condition near the while, which is almost the same thing as a hurdle that is put in on the track. The runner has to pass this hurdle each time he completes one lap around the track, that's your loop. Your loop has to pass this check every single time it starts.</p>

<p>Then I give an example:</p>

<pre><code>While (People are watching)
    Run()
</code></pre>

<p>The runner will stop after all the spectators have stopped watching him at the start of the next loop. He will get to the hurdle and notice that nobody is watching him and he will lose all motivation and then fall over.</p>

<p>Now if we want to tell the runner to run forever, we just have to make the hurdle so low that it can't possibly be false:</p>

<pre><code>While (You are running)
    Run()
</code></pre>

<p>This is the same thing as the following:</p>

<pre><code>While (True)
    Run()
</code></pre>

<p>Of course, this isn't formatted for python or anything else, you can modify this to suit whatever coding language you're teaching.</p>

<p>I hope this helps someone.</p>

CC BY-SA 3.0
2018-01-31T20:15:50.590Z
Welcome to CSEducators. An interesting metaphor.
Buffy
CC BY-SA 3.0
2018-01-30T15:27:34.740Z
2018-01-30T15:27:34.740Z
John Hamilton
<h1>In an ideal teaching language</h1>
<p>If the language directly supports them then the order to teach looping constructs seems simple.</p>
<ul>
<li>Infinite loop: <code>forever</code>.</li>
<li>Simple finite (bounded) loop: <code>repeat n times</code>.</li>
<li>Bounded loops: <code>for i in list</code> or <code>for i = 1...6</code> (I am not sure which order these two should be in).</li>
<li>Simple unbounded: <code>while condition</code></li>
<li>Complex loops: all the rest.</li>
</ul>
<p>However often the teaching language does not directly support these. This may be an argument for choosing a different language for teaching programming.</p>
<h1>The problems that I am having</h1>
<p>(I am teaching in Python).<br />
I was teaching in the order I outlined for an ideal language, but are getting problems.</p>
<p>For an infinite loop we use <code>while True</code> in Python, however students ask or struggle with this. I can not explain it without explaining <code>while condition</code>. So pupils have to hold something that they do not understand in their head.</p>
<p>Similar for <code>repeat n times</code> in Python it is <code>for n in range(5):</code>, this is a lot to keep in ones head without understanding.</p>
<h1>The question</h1>
<p>So the question is “what to do?”. Do we start with</p>
<ul>
<li>Bounded loops: <code>for i in list:</code></li>
<li>Bounded loops: <code>for i in range(n):</code></li>
<li>Simple unbounded: <code>while condition:</code></li>
</ul>
<p>Or do we do it some other way?</p>

CC BY-SA 3.0
2018-01-29T10:42:40.227Z
This question is related, but different https://cseducators.stackexchange.com/q/4241/204
ctrl-alt-delor
CC BY-SA 3.0
2018-01-29T13:24:06.377Z
Since languages are defined recursively, it is pretty hard to teach them strictly linearly so that _nothing_ ever needs to be passed forward. This is one (not the most important) reason for a Spiral approach to teaching in which topics are first introduced in a simple way and then returned to later, both reviewing and deepening the knowledge.
Buffy
CC BY-SA 3.0
2018-01-29T19:09:56.693Z
You could instead use `for(;;)` as your infinite loop example if you want to start with `for` loops.  It might be easier to explain that an "infinite" loop is one that doesn't have an exit condition at the top.
IllusiveBrian
CC BY-SA 3.0
2018-01-29T19:37:16.960Z
@IllusiveBrian python does not have `for(;;)`, and as you say I just have to explain that it has no exit condition, so then have to explain exit condition. Though a different approach of saying that the `;` are separators, we will look at what goes in the gaps latter, my be preferable. As there is nothing for the students to try to ignore. It is easier to ignore nothing. Unfortunately this is not an option in python.
ctrl-alt-delor
CC BY-SA 3.0
2018-01-30T13:11:43.843Z
In my school, we started with C and for loop was forbidden. You dont need it so dont use it.
aloisdg
CC BY-SA 3.0
2018-02-02T03:26:19.593Z
I started in a language that only had "goto". All other structures can be expressed and are ultimately executed in that paradigm.
pojo-guy
CC BY-SA 3.0
2018-02-02T09:45:20.357Z
@pojo-guy Back to to low-level first or high-level first. see https://cseducators.stackexchange.com/q/3696/204
ctrl-alt-delor
CC BY-SA 3.0
2018-02-02T16:03:16.480Z
@ctrl-alt-delor Yep, it's a conumdrum. In an ideal world you would teach both simultaneously.  I can already see the steam coming from the ears of prospective future programmers though.
pojo-guy
<looping><lesson-order>
Order to teach looping constructs in a non ideal language
# Order to teach looping constructs in a non ideal language

(Asked by: username_0 on 2018-01-29)

# In an ideal teaching language

If the language directly supports them then the order to teach looping constructs seems simple.

* Infinite loop: `forever`.
* Simple finite (bounded) loop: `repeat n times`.
* Bounded loops: `for i in list` or `for i = 1...6` (I am not sure which order these two should be in).
* Simple unbounded: `while condition`
* Complex loops: all the rest.

However often the teaching language does not directly support these. This may be an argument for choosing a different language for teaching programming.

# The problems that I am having

(I am teaching in Python).  
I was teaching in the order I outlined for an ideal language, but are getting problems.

For an infinite loop we use `while True` in Python, however students ask or struggle with this. I can not explain it without explaining `while condition`. So pupils have to hold something that they do not understand in their head.

Similar for `repeat n times` in Python it is `for n in range(5):`, this is a lot to keep in ones head without understanding.

# The question

So the question is “what to do?”. Do we start with

* Bounded loops: `for i in list:`
* Bounded loops: `for i in range(n):`
* Simple unbounded: `while condition:`

Or do we do it some other way?

**Tags:** looping, lesson-order

> **Comments:**
> **username_0**: This question is related, but different https://cseducators.stackexchange.com/q/4241/204  
> **username_1**: Since languages are defined recursively, it is pretty hard to teach them strictly linearly so that _nothing_ ever needs to be passed forward. This is one (not the most important) reason for a Spiral approach to teaching in which topics are first introduced in a simple way and then returned to later, both reviewing and deepening the knowledge.  
> **username_2**: You could instead use `for(;;)` as your infinite loop example if you want to start with `for` loops.  It might be easier to explain that an "infinite" loop is one that doesn't have an exit condition at the top.  
> **username_0**: @username_2 python does not have `for(;;)`, and as you say I just have to explain that it has no exit condition, so then have to explain exit condition. Though a different approach of saying that the `;` are separators, we will look at what goes in the gaps latter, my be preferable. As there is nothing for the students to try to ignore. It is easier to ignore nothing. Unfortunately this is not an option in python.  
> **username_3**: In my school, we started with C and for loop was forbidden. You dont need it so dont use it.  
> **username_4**: I started in a language that only had "goto". All other structures can be expressed and are ultimately executed in that paradigm.  
> **username_0**: @username_4 Back to to low-level first or high-level first. see https://cseducators.stackexchange.com/q/3696/204  
> **username_4**: @username_0 Yep, it's a conumdrum. In an ideal world you would teach both simultaneously.  I can already see the steam coming from the ears of prospective future programmers though.  

---

## 7 answers

### Answer by username_6 on 2018-01-29. Score: 5 | Accepted answer

While your ideal order makes sense, you really should try to plan the course based on the limitations of the language that you're working with. In this case, instead of thinking about the different conceptual loops, you should think about the different loop syntaxes that the language provides.
In this case, the order in @username_7's answer (if/else -> while -> arrays -> for) makes the most sense to me. Then, once you've finished teaching all the different sytaxes for loops, you can talk about how some loops are equivalent, for example:
a = 2
i = 0
while i < 5 do
  a = a*2
  i = i+1
end
# Ends with a == 64

is the same as
a = 2
for (i = 0; i < 5; i++) {
  a = a*2;
}
# Ends with a == 64

(also, see my answer to a different loop-related question about how for loops and while loops are really the same thing)
and then show them your ideal loops, because those are a far better way to classify different types of loops than based on syntax alone, but they won't be able to see the importance of that classification until they are comfortable with the syntax for the different loops.


---

### Answer by username_7 on 2018-01-29. Score: 17

I prefer to teach `while` loops as part of my unit on `if`/`else` statements since the syntax and thought process are virtually identical.

I then introduce `for` loops when teaching lists (in Python) or arrays (in Java). With `for` loops, language impacts which style you likely use first. With Python, I use the `for item in list` structure first. In Java, I use the C style `(for i=0; i<...)` loop first.

I don't try to introduce all styles of looping at once as I prefer to develop the concepts across units.

> **Comments:**
> **username_8**: I've seen `for`-loops introduced after seing a lot of examples of `while`-loops with a counter that is incremented once per loop, and seing the utility of a more compact way to do that.  
> **username_0**: Makes sense. I was thinking that with an ideal language I would teach iteration before conditional. Conditional code is over used, in most programs that I have seen. I have also done a proof that you can do any thing (Turing complete) without conditionals, so long as you can loop. (also you only need 2 loops: the super loop and the container iterator). Try it for a challenge.  

---

### Answer by username_9 on 2018-01-29. Score: 8

All of the IF and Loop structures are based on two fundamental operations: Test and Branch. The only difference between an IF-Else and a Loop of any kind is whether you branch downward or back to the top. On a flow chart this just knocks you in the nose: the only difference is the arrowhead on one line.

When something so varied and complex can be reduced to such simplicity, you know that it is a fundamental idea. Teach that idea first, and then show how it is implemented in all of the various structures as you introduce each one. Now your students understand that programming is not magic, it is not about memorizing incantations and syntax: it is about simple, logical analysis of a problem, exactly how people and animals have been breaking down problem solutions in to basic steps all along. There is nothing new or surprising here, and it need not be confusing.

With this understanding, your students can absorb new structures and languages themselves, knowing that **it has to be simple underneath**, because circuits are simple, and people are not really able to do vastly complex things without breaking them down in to steps. Test. Branch. Done.

> **Comments:**
> **username_6**: This is a very interesting answer. Welcome to [cseducators.se]! My only suggestion is that it might be helpful for you to draw a more direct connection between the question and the answer.  
> **username_10**: I really like this perspective, and I hadn't quite thought about it stated so simply and cleanly before.  (I also don't generally teach absolute beginners, so I don't have a ton of experience having to clarify this particular idea.)  This also puts a nice punctuation mark on username_7.'s [observation](https://cseducators.stackexchange.com/a/4271/27), "I prefer to teach `while` loops as part of my unit on `if`/`else` statements since the syntax and thought process are virtually identical."  
> **username_0**: While I agree that this is how they are usually implemented. This is back to the structured/un-structured high/low level argument, that you were not here for. (yes I said usually: for example on the ARM — the most popular ≥ 32bit cpu on the planet at this time — conditional and iteration are implemented different at the very low level. For a backward conditional branch of a loop the branch predictor assumes that the branch will happen. For a forward conditional of an if it is assumed not to happen. Arm also has conditional instructions. )  But thanks for contributing.  
> **username_11**: "*The only difference between an IF-Else and a Loop of any kind is whether you branch downward or back to the top*" just isn't true.  DO...WHILE and LOOP...UNTIL and FOR X UNTIL Y STEP Z are nothing but (very useful) syntactic sugar laid over TEST and BRANCH.  
> **username_10**: @username_11 What you said is exactly what I got out of what username_9 posted. He also said that both if/else and all of the loop constructs are just test and branch.  What are you saying that is different?  
> **username_11**: @BenI. I directly quoted this part that isn't true.  Specifically, "**whether you branch downward or back to the top**"  Then I explained why.  Maybe I didn't do a good enough job, though: you can have loops that check the condition at the top (WHILE...DO) and you can have loops that check the condition at the bottom (DO...WHILE).  However, C and it's derivatives don't have DO...WHILE, so it seems to have fallen out of favor.  I agree with everything else that username_9 (and username_7) wrote.  
> **username_10**: @username_11 Oh, I see.  The quibble was about whether the jump upwards was as a result of the test, or whether it was an unconditional jump.  It still seems to reduce to the same basic idea: loops involve a jump backwards while `if`s involve a jump forwards.  
> **username_11**: @BenI. Correct, that's my disagreement.  Here's what I'm talking about:  https://en.wikipedia.org/wiki/Do_while_loop and https://en.wikipedia.org/wiki/While_loop  
> **username_12**: @username_11 Hm, C most certainly has a DO...WHILE construct. Did you mean that Python does not have it?  
> **username_11**: @username_12 you're absolutely right.  It's been a while since I've used C, and forgot about it.  
> **username_13**: The importance of this answer is that it emphasizes underlying concepts. I think it can *help*, not only hurt, to teach this first - *un*structured control in various forms, and then teach structured control. That presents an excellent opportunity to teach **why** structured control is preferable. IOW, more or less follow the history -- the history itself evolved the way it did for good reasons. It's not just about teaching the use of a particular language; it's about teaching what's going on - *why* we do things the way we do. The various iteration constructs did not fall from the sky.  
> **username_14**: We do iterations, not for the pleasure to do so, but as a way to compute some results (say average of an array). Unstructured control is what the computer can do, at its lower level. But what we do goes from "divide sum by size" then "compute sum by adding all values" then "use a variable with initial value 0 and add each values", and "range over all array elements" and "use an index starting from 0 etc" (if we dont have a foreach loop over collections) and LAST use a for/while/if-goto loop. We jump from the sky.  

---

### Answer by username_1 on 2018-01-29. Score: 4

Perhaps you are creating a problem where none exists. You seem to be assuming that looping needs to be covered all at once and all together, rather than distributed over some some longer range interspersed with other topics.

The two fundamentally different kinds of looping are *definite* in which the program knows how many iterations are necessary prior to the loop and *indefinite* in which it does not. Those two concepts can be taught back to back so that the essential concepts are there. The others can wait a bit. Looping over a range or list can wait until you teach collections, for example. Infinite looping can be taught as either definite or indefinite (while True:...) depending on your language.

I certainly would *not* choose a language based solely on its direct syntactic support for all of the possible loop forms. Most of them are excluded, in fact, to keep the number of keywords in a language to a reasonable number. Once the language is Turing Complete things can be built using idioms of the language and a student needs to become familiar with that idea (idiomatic programming) in any case.

In any case, looping, like everything else should be taught in a Spiral framework in which ideas are introduced correctly but incompletely early on and then returned to, reinforced and expanded later on. This is useful when teaching programming since languages are themselves recursive and so difficult to teach strictly linearly. But a Spiral pedagogical technique is also essential due to the way the mind works and the way people learn - via reinforcement and repetition.

Therefore simple ideas of looping can come early and more complex ones later. The level of explanation changes, since the early, simple, explanations fertilize the ground for the later ones, both expanding and deepening the knowledge. *Spiral* is one of the fundamental [Pedagogical Patterns](https://smile.amazon.com/Pedagogical-Patterns-Educators-Joseph-Bergin/dp/1479171824).

> **Comments:**
> **username_0**: You are correct that we should not do it all at once. While I have not mentioned, the other thinks that could be interleaved, please do not take this of evidence that I would not interleave other learning.  
> **username_0**: I have added a section to question to explain the problem.  

---

### Answer by username_15 on 2018-01-30. Score: 3

For an infinite loop we use while True in python, however students ask or struggle with this. I can not explain it without explaining while condition. So pupils have to hold something that they do not understand in their head.

Could you restructure this to something that makes sense to your students in plain English?
Something like:
programShouldRun = True

while programShouldRun:
    doStuff()

Call your boolean variable whatever makes sense in the context you're working in, whether that is programShouldRun, answerNotFound, fileStillHasLines, etc.

> **Comments:**
> **username_1**: Hmmm. If `programShouldRun` is a variable then this isn't specifically an infinite loop. It may turn out to be, but that depends on the future possible value of the variable. Constants are possible in Python, but a deeper concept and language specific, of course.  
> **username_15**: Yeah, that's definitely possible, though on the other hand that could be used as a lead-in to the difference between an infinite loop and one that eventually terminates.   "Up until now we've said that `programShouldRun` is always `True`, but what do you think happens if we set it to `False` at the bottom of the loop?"   Alternatively: "So, this program runs forever, but we want to exit when [thing we are looking for]. Does anyone have any idea about how we'd go about fixing that?"  
> **username_1**: But don't miss the point that the question was about _classifying_ loops. An infinite loop should clearly say, semantically or syntactically, that "this loop is infinite". Controlling it specifically with a variable says just the opposite. It is about the meaning of the code, not just about how it plays out. Students (and professionals) often write infinite loops by mistake, forgetting to change a condition in some way. Your technique works only in a language that can clearly specify a constant value. For example an alternate symbol for True.  

---

### Answer by username_16 on 2018-01-30. Score: 2

I only ever taught my fellow students but this method was the one that everyone seemed to get.
There's a track, and there's a runner, he's running forever. When you type that while in, you're telling him to do something while he runs. Then you put a condition near the while, which is almost the same thing as a hurdle that is put in on the track. The runner has to pass this hurdle each time he completes one lap around the track, that's your loop. Your loop has to pass this check every single time it starts.
Then I give an example:
While (People are watching)
    Run()

The runner will stop after all the spectators have stopped watching him at the start of the next loop. He will get to the hurdle and notice that nobody is watching him and he will lose all motivation and then fall over.
Now if we want to tell the runner to run forever, we just have to make the hurdle so low that it can't possibly be false:
While (You are running)
    Run()

This is the same thing as the following:
While (True)
    Run()

Of course, this isn't formatted for python or anything else, you can modify this to suit whatever coding language you're teaching.
I hope this helps someone.

> **Comments:**
> **username_1**: Welcome to CSEducators. An interesting metaphor.  

---

### Answer by username_5 on 2018-01-29. Score: 1

Maybe you could use physical objects to help students visualize how to utilize these loops.

For example for the bounded loops: grab 5 apples, label them with some number, and show how you go through each apple (e.g., bite each of them one at a time).

For the simple unbounded, try this: put a set of brand new apples on the table, ask if there are any of them that haven't been bitten yet. If so, then bite one, then ask again and bite another non-bitten apples until all of them are bitten.

> **Comments:**
> **username_0**: Some good ideas, however I am looking for advice on order to teach loops in a non-ideal language.  

---

