Lesson 10.2: Types of Loop
In the previous section of this lesson we took a look at the basics of loops. In this section we’ll take a look at five specific types of loops that are commonly found in programming languages: for, while, do…while, foreach, and do…until.
For Loops
A for loop is a loop that makes use of an iterator variable, and will add to this iterator to help it reach a certain point, at which time it stops. An iterator is just a fancy term for a variable (in this case, an integer), which is used to “iterate” or do over and over, until it reaches an end point.
The structure of a for loop varies from language to language, but the basics are the same. It contains three parts: initialization of the iterator, the condition, and the increment of the iterator. The variable name “i” is often used for for loops (i is short for iterator). Here is an example:
For Integer i = 0; i < 5; i = i + 1
Print i
End For
The first part, Integer i = 0, declares the variable i and then initializes (sets it up) to 0. The next part, i < 5, is the actual condition in our loop. This loop will continue to run as long as i is less than 5. Then final part is where we increment (increase) i by one.
This loop would start up with i = 0. It would then print "0". It would then increment i by 1, so i = 1. It would then print "1". Increment i by 1, so i = 2, print "2", and the same for 3 and 4. At the end of the loop, it would increase i to 5. At that point, the loop would stop, and it would run any code that is found below the loop.
While Loops
While loops take a condition and continually loop as long as that condition is true. Here is an example:
Boolean busy = true
While busy = true
If User pressed Enter
busy = false
Print "You're still busy!"
End While
This little example would be exceedingly annoying if you were to actually implement it in a program. It would continually print "You're still busy!" as long as busy remained equal to true, as fast as it possibly could. Once the user pressed the enter key, it would set busy to false. At that point, it would stop printing.
You can also use iterators with while loops as well. The for loop from above can be rewritten as a while loop, like so:
Integer i = 0
While i < 5
Print i
i = i + 1
End While
Notice that we initialize our integer outside of the while loop. We then go through the loop and print i, then increment it in the same fashion as we did in the for loop.
You can use this simple sentence to remember while loops:
"While some condition is true, do some action."
The general rule when deciding whether to use a for loop or a while loop is that you use a for loop if you are going to do something a known finite number of times and a while when you aren't certain (or you're going to do it an infinite number of times). Game loops I discussed back in our initial section about Conditional statements would be while loops because they'll run indefinitely as long as the program is open.
Do...While Loops
Do...While loops are very similar to While loops. The only difference is that a Do...While loop will always run at least once, before it checks the condition, even if the condition is false at the start. A Do...While loop looks like this:
Integer i = 0 Do Print i i = i + 1 While i < 5
Notice that the while portion comes at the end of the do...while, instead of the beginning. In this case, the result of this do...while loop will be no different from the while loop.
You can say a do...while loop as a simple sentence:
"Do some action while some condition is true."
Do...While loops are useful for when you need to setup something the first time you run the loop, or for when you need to make sure that the statement is run at least once, and then potentially more times depending on what the user does.'
Do...Until
The do...until loop is like the do...while loop, except it does it until the condition becomes true. Here is an example of a do...until that will have the same result as our other loops:
Integer i = 0 Do Print i i = i + 1 Until i >= 5
This will give us the same output as the others. Note how the condition is basically the opposite of our Do...While condition. The do...while condition was i < 5; the do...until condition is i >= 5.
You can say a do...until loop as a sentence:
"Do some action until some condition becomes true."
You can write the equivalent of a do...until loop with a do...while loop by simply making the condition Not something.
Do
Print i
i = i + 1
While Not i >= 5
Foreach
Foreach loops are a type of loop that generally deals exclusively with Arrays. If you don't remember, an Array is basically a collection of another type of variable. For example, let's say we have an array of integers named myArray, with the values {1, 2, 3, 4, 5}. A foreach loop will go through and perform some action for each of the elements in the array.
So, here is an example using the array we specified above:
Foreach value in myArray
Print value
End Foreach
This would go through each value in myArray and print it out. Notice that we say "value in myArray". This means that we are going to refer to the current array element as "value" within our foreach loop. We just print out the value in this example.
Foreach loops are very handy if you need to do something to every element in an array.
You can remember foreach loops by saying:
"For each element in an array, do some action."
Some languages don't contain a foreach loop. In those languages, it's common to use a for loop instead (because remember, we generally use for loops for loops of finite size, which an array is in general). Here is an equivalent of the previous foreach statement. The "myValue.length" will give us the length (the number of elements) of the array. In our example, it would be 5. Also, "myArray[i]" means we are looking at the i-th element in the array.
Integer length = myArray.length;
For Integer i = 0; i < length; i = i + 1
Print myArray[i]
End For
In this section we finished up our lesson on loops by looking at five common types of loops and discussed their uses. In our next lesson we'll take a look at functions, which are one of the most vital components to modern programming.




