I’m learning the basics of programming here (C#) but I think this question is generic in its nature.
What are some simple practical situations that lend themselves closer to a particular type of loop?
The while and for loops seem pretty similar and there are several SO questions addressing the differences between the two. How about foreach? From my basic understanding, its seems I ought to be able to do everything a foreach loop does within a for loop.
1. foreach and for
A
foreachloop works with IEnumerator, when aforloop works with an index (inobject myObject = myListOfObjects[i], i is the index).There is a big difference between the two:
an index can access directly any object based on its position within a list.
an enumerator can only access the first element of a list, and then move to the next element (as described in the previous link from the msdn). It cannot access an element directly, just knowing the index of the element within a list.
So an enumerator may seem less powerful, but:
So actually the big strength of the
foreachloop and the underlying use ofIEnumeratoris that it applies to any type which implementsIEnumerable(implementing IEnumerable just means that you provide a method that returns an enumerator). Lists, Arrays, Dictionaries, and all other group types all implement IEnumerable. And you can be sure that the enumerator they have is as good as it gets: you won’t find a fastest way to go through a list.So, the
forloop can generally be considered as a specializedforeachloop:is perfectly equivalent to:
I said generally because there is an obvious case when the
forloop is necessary: when you need the index (i.e. the position in the list) of the object, for some reason (like displaying it). You will though eventually realize that this happens only in specific cases when you do good .NET programming, and thatforeachshould be your default candidate for loops over a group of elements.Now to keep comparing the
foreachloop, it is indeed just an eye-candy specific while loop:is perfectly equivalent to:
The first writing is a lot simpler though.
2. while and do..while
The
while (condition) {action}loop and thedo {action} while (condition)loop just differ from each other by the fact that the first one tests the condition before applying the action, when the second one applies the action, then tests the condition. Thedo {..} while (..)loop is used quite marginally compared to the others, since it runs the action at least once even if the condition is initially not met (which can lead to trouble, since the action is generally dependent on the condition).The
whileloop is more general than theforandforeachones, which apply specifically to lists of objects. Thewhileloop just has a condition to go on, which can be based on anything. For example:asks the user to input his name then press Enter, until he actually inputs something. Nothing to do with lists, as you can see.
3. Conclusion
When you are going through a list, you should use
foreachunless you need the numeric index, in which case you should usefor.When it doesn’t have anything to do with list, and it’s just a procedural construction, you should use
while(..) {..}.Now to conclude with something less restrictive: your first goal with .NET should be to make your code readable/maintainable and make it run fast, in that order of priority. Anything that achieves that is good for you. Personally though, I think the
foreachloop has the advantage that potentially, it’s the most readable and the fastest.Edit: there is an other case where the
forloop is useful: when you need indexing to go through a list in a special way or if you need to modify the list when in the loop. For example, in this case because we want to remove every null element from myList:You need the
forloop here because myList cannot be modified from within aforeachloop, and we need to go through it backwards because if you remove the element at the position i, the position of all elements with an index >i will change.But the use for these special constructions have been reduced since LINQ. The last example can be written like this in LINQ for example:
LINQ is a second step though, learn the loops first.