I've seen tutorials and textbooks that teach conditional loops (while) first and I've seen others that teach iterative (for) loops first. Have there been any studies done as to which produces better results as far as student learning or is it a matter of personal taste?
|
Teach the while loop first, since a for loop can be rewritten as a while loop for your students:
Then you can go over the pros/cons of each. |
|||||||||||||||||||||
|
|
For newcomers to programming, the A The fact that you can use a Update: Answer assumes C style languages (since that was the original phrasing of the question). But even for non C style loop, I believe conditional loops are easier to grasp (keep doing something until a condition changes), versus an iterative loop. Admittedly in some languages the mental jump is not as large as in C style languages - in these cases if an iterative loop reads more like the native language, it may be the easier one to learn. |
|||||||||||||||||||
|
|
The for loop is a special case of a while loop. The for loop is the basic mechanism that allows to define primitive recursive functions whereas the more general while loop allows to define all computable functions - μ-recursive functions. So an approach can be to first introduce the for loop as a way to iterate over a well-defined sequence of items (numbers, elements of a finite list, etc), and then look at the more general while loop (repeating a program block as long as a certain condition is true). Also, one should explain the difference between a more traditional for loop as found in Pascal
and a C-style for loop
which is rather some kind of while loop with built-in initialization (it can be used to define an iteration but also as an alternative syntax for a while loop). An important difference is that a Pascal-style for loop always terminates, whereas the more powerful while loop may run forever; so a buggy while loop can hang your program. (''With great power comes great responsibility...''). |
||||
|
|
While I am unaware of studies regarding which one is best, I believe while offers less complexity: it is truly a loop that continues while the condition is true. In the case of for, depending on the language, you can have extra complexity. Usually it is used to loop through collections (whether you use an iterating index, or you use a "for x in list" type of loop), so you have to make sure you have covered those first. |
|||
|
|
|
There is not much difference between teaching/understanding the complexity between "for" or "while" conditional looping statements. Please make sure to teach the difference between these statements and the when should one be used over other. Both statements are very simple and students will pick up easily regardless of the order you teach. |
||||
|
|
In Python, I would teach the |
|||
|
|
|
Purely technically speaking, "iterative" or "conditional" depends on how you use the loop construct. Both
I doubt anyone would ever "study" this kind of thing, because it's trivial. The loop construct is very simple (especially compared to other stuff students are usually required to understand, like recursion, pointers or OOP), and is usually comprehended fairly quickly and naturally, from my academic experience (i.e. communicating with other students). In all honesty, it really is a matter of subjectivity. Both constructs are simple. For some, one could be easier - for others, the other. |
|||||||||||
|
|
It is best to teach if and goto first because that is the basis. All other control structures are syntactic sugar for if and goto, with compiler-generated labels and hidden variables. I would encourage the students to be as clever as possible with their ifs and gotos. Do whatever it takes to solve the problem. Structured programming is encumbered with rationale that may be over the newbies' heads and whose benefits (obvious to you and me) may not be so clear. But their minds may be ready to receive the rationale after struggling with a bit of if/goto spaghetti code for a few weeks. |
|||
|
|
I would say teach both as none of it is too complicated and then leave it on the student to take what he feels makes more sense and easy to grasp. In my case for loop was the one that made more sense and made me comfartable initially and since then i have always used for loop even though i rarely use while but it automatically makes sense after the student gains more experience. |
|||
|
|
|
I think this is pretty language-dependent. In C, i'd say that that In Python, on the other hand, the task of looping over "some things" is easier to understand with a
I think, is much easier understand/explain than the Even the |
|||
|
|
|
I would say it depends upon the language - if It loops over a range, from/to then introduce the for loop first, as that is a familiar construct. Likewise if it has a for each and you've already introduced collections. If the for loop doesn't offer any real advantage over a while loop, then go with the while loop. |
|||
|
|
|
It really depends on the language. In languages like Python with a for-each style loop beginning with the for-loop makes more sense because they can be introduced in combination with the built-in data structures and are used significantly more often than while loops. In languages with C style for loops I would begin with while loops because they are significantly simpler to understand. |
|||
|
|

start,endand possibly astepbut no explicit condition 3) foreach. Which one are you talking about? – CodesInChaos Apr 18 '12 at 15:16