Looping Structures
October 3rd, 2009
No comments
Looping is a common concept in any programming language. It is commonly used to loop through a group or list of common elements. Before we start though, I want to point out that loops can be used for many different things.
Well lets begin, there are 3 basic looping structures, of which are explained below.
- While: A while is commonly used when you do not know the size of the list. For example when you are reading in a file. These are read in many different ways, line by line, character by character, etc. Any way you look at it though, at the beginning of the loop, you do not know how many reads you are going to have to execute. There could be a range of zero to infinity.
- do while: A do while is not as common. It is used for the exact same reason as the while loop, except its code will always be executed at least once.
- For: I personally use this loop the most. It is used when you know the exact size of the list. For example. If you are looping through the english alphabet. You know there is going to be 26 elements in the list, so you should use a for loop.
I hope this tutorial has helped you understand the basics of looping. If you have any questions, please leave comments below and I will update this post!
Categories: Programming Fundamentals