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
orfor 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?
for(;;)
as your infinite loop example if you want to start withfor
loops. It might be easier to explain that an "infinite" loop is one that doesn't have an exit condition at the top. $\endgroup$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. $\endgroup$