Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
1  
I think students should just be taught about loops. They ought to be taught to recognize each pattern and apply the appropriate structure. I don't think it matters which one comes first. –  zzzzBov Apr 17 '12 at 23:41
2  
There are at least three different kinds of for-loop. 1) C style (general while loop with inline initialization/modification) 2) Pascal/Basic style with start, end and possibly a step but no explicit condition 3) foreach. Which one are you talking about? –  CodesInChaos Apr 18 '12 at 15:16
add comment

12 Answers

up vote 21 down vote accepted

Teach the while loop first, since a for loop can be rewritten as a while loop for your students:

i = 0
while i < 10 {
  i++
}

Then you can go over the pros/cons of each.

share|improve this answer
9  
Teach the for loop first, since a while loop can be rewritten as a for loop for your students: for (; condition;) { doStuff(); } Then you can go over the pros/cons of each. –  zzzzBov Apr 18 '12 at 3:41
10  
Teach recursion first, since both techniques can be rewritten as recursion for your students: function for(n) { if(n > 0) { doStuff(); foo(n-1); } else { doStuff(); } } –  Christian Mann Apr 18 '12 at 3:58
20  
@ChristianMann The problem with teaching them recursion is that first you have to teach them recursion. –  Jake Apr 18 '12 at 4:04
4  
If the class is using a C-like language, it's probably worth mentioning that Kernighan and Ritchie explicitly defined the "for" loop in C as a bit of syntactic sugar around "while." –  mjfgates Apr 18 '12 at 5:07
1  
Agree, plus the for loop is clunky, the while loop makes the behaviour much more explicit. –  boisvert Apr 18 '12 at 9:58
show 5 more comments

For newcomers to programming, the while loop is easier to understand, so when introducing looping it makes sense to start with that.

A for loop is much more complicated - it is made up of three parts and as such is more of a struggle for students.

The fact that you can use a for loop as a while or do loop is also easier to explain if the students have had exposure to the simpler loops.

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.

share|improve this answer
2  
What about something like Ruby's x.5.times? –  World Engineer Apr 17 '12 at 19:28
3  
@WorldEngineer - What about it? The question specifically mentions for and while, so the assumption that these are c style constructs that are being asked about is reasonable. Ruby, not being of that style is out of scope. –  Oded Apr 17 '12 at 19:29
2  
+1. When you introduce while loops, you can show a whole loop with a counter and rewrite it as a for loop. –  Atif Apr 17 '12 at 19:35
    
I've edited the question to be more in line with my original intent which is broader than C based languages. Apologies on any confusion. –  World Engineer Apr 17 '12 at 19:35
2  
what about for-each style loops? I've talked to a number of non-programmers and they find those to be the easiest to understand (no exit conditions or indexes) –  Steve Evers Apr 17 '12 at 19:53
show 4 more comments

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

FOR var := FirstValue TO LastValue DO

and a C-style for loop

for (T var = FirstValue; <predicate>; <expr>)

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...'').

share|improve this answer
    
+1 for not only offering an opinion but giving a well-founded reason. –  Konrad Rudolph Apr 18 '12 at 10:32
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
    
I think there is a big difference between for and while loops: in many (most) languages a for loop always terminates whereas a while loop might loop forever (program hangs). This is IMO a huge difference. –  Giorgio Oct 23 '12 at 13:20
add comment

In Python, I would teach the for loop first since in my opinion, it's "more basic".

share|improve this answer
add comment

Purely technically speaking, "iterative" or "conditional" depends on how you use the loop construct. Both while and for can be either conditional or iterative.

for (;true;);

int i=0;
while(i++<10);

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.

share|improve this answer
    
Why not for(;;); –  alternative Apr 18 '12 at 0:23
    
@alternative I wanted to illustrate the condition part. –  Yam Marcovic Apr 18 '12 at 5:48
    
@YamMarcovic, if we want to illustrate the parts of a loop, both your examples are broken. I always tell students a loop needs three parts: how to start, how to stop (or how long to continue), and how to move from a step to the next. A good example has to make all three transparent. –  boisvert Apr 18 '12 at 10:02
    
@boisvert I wasn't suggesting a way to teach loops though, I was suggesting that understanding loops is trivial and that there isn't a lot of difference between these kinds of loops. –  Yam Marcovic Apr 18 '12 at 16:18
    
@YamMarcovic but that's the thing - loops aren't trivial, or not to the majority of learners in any case. The two techniques are equivalent, but the easier syntax is still needed. –  boisvert Apr 18 '12 at 16:26
show 1 more comment

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.

share|improve this answer
    
I came here to say this. I think most developers these days are too young to remember having to use GOTO like this because it was your only option in a lot of languages. –  blesh Apr 19 '12 at 15:31
add comment

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.

share|improve this answer
add comment

I think this is pretty language-dependent.

In C, i'd say that that while loops are quite easier to understand than for loops, because they only do one thing: looping while a condition is verified. Then the for loop can be seen as a compact transformation of a while.

In Python, on the other hand, the task of looping over "some things" is easier to understand with a for loop than a while. This:

for s in ["looping", "over", "things"]:
    print(s)

I think, is much easier understand/explain than the while equivalent: "the code inside the loop is executed once per element, s, in the list". That's it; no loop condition that changes every time; no "calculating" how many times the loop is going to run ("3 times! as there are 3 elements in the list"); you know when the loop ends (this is pretty important, i think, as you avoid the possibility of your students falling on an infinite loop the first time they try to make a loop hehe).

Even the for n in range(0, 10) is easier to grok than an equivalent while in my opinion, but i think starting with static list literals as "things to loop" should be even easier :)

share|improve this answer
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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