As prior discussed, programming can be a prosaic and tedious task when it comes to repeating a segment of a program over and over again. Common programming tasks, as learned in the prior units, involves repeating one set of instructions a delimited by the keywords loop and end loop instead of long lists of character strings. Another variation of the basic loop statement is the for loop, often used to simplify code. The for loop (also colloquially called a counted loop) is a cycle which repeats the instructions a specified number of times without using neither an exit statement nor a counter or accumulator. In fact, you do not even go through the trouble of initializing the variable! In this type of loop, the loop statement starts with the keyword for and ends with the keyword phrase end for.
The basic structure of the for loop is simple. It is composed of 3 essential parts:
- Control Variable
A variable that is immidiately followed by the 'for' keyword and is preceded by a colon and a range. The control variable is never declared outside the for loop as it is a local variable.
- Starting Value
- Ending Value
The two values above - starting value and ending value - are very lenient in terms of their expression. The Turing programmers allowed them to be one of the following forms:
- literal constant
- declared constant
- global variable
- variable whose value is user-supplied
The complete basic structure of a for statement is similar to the following:
for controlVariable : startingValue .. endingValue % zero or more statements end for
Some key information when coding, executing and de-bugging for loops is that the control variable in a for loop may not be declared because it only belongs to the for loop, and is never used outside of it. Therefore, since the variable is used solely inside the counted loop, the variable is classified as a local variable.
How the Counted Loop Works
How the for loop works is very simple. When the for loop is executed, the computer initializes the control variable the value of the starting variable. Once initialized, the control variable is then compared to the ending value and if the control variable is equivalent or less then the ending value, the statements within the for loop are executed. Once the for loop statements are performed, the control variable is incremented by one and yet again compared to the ending value.
As the programmer, you have the ability to change the number incremented by. The way you increment the number is that beside the ending value, you use the keyword 'by' followed by another ordinal value. As well as an incrementing for loop, there also decrementing for loops further described in the unit.
Example of a Counted Loop
To do this, we will use a counter called the control variable, which is contained by the loop itself. They are also initialized and declared by it. A common way of making counted loops is through the keyword for, which gives the initial value as well as the ultimate value for the control variable.
- Shortening Long Prorams
- Counting
The following is a short program counting from one to ten:
for countingIntegers : 1 .. 10 put countingIntegers end for
In this example, the control variable countingIntegers is first initialized with the value one. Then it tests to see if the final value (10 in this case) is reached. If it is, the loops end, if not, then the loop instructions are executed and the control value is incremented by 1. Then, repeat.
Please note that there is also another way to do counted loops without the for statement. The following program fragment uses accumulators that are conventionally declared instead of counters:
loop exit when countingIntegers = 10 put countingIntegers countingIntegers := countingIntegers + 1 end loop
This allows greater freedom as you could increment by any value, even other variables!