One of the more valued features of computers is their ability to repeat, and repeat often. The loop is the crucial language construct underlying concept. Would you like to compute the conversion table from Celsius to Fahrenheit for every value between -100 and 100? Here's how to automate that.
int main Array argv { float celsius = -100.0; while (celsius <= 100.0) { float fahrenheit = 32.0 + 9.0 / 5.0 * celsius; [[[stdio out] print (celsius, " ", fahrenheit)] nl]; celsius = celsius + 1.0; } return 0; } |
The while loop tests its condition, in this case whether celsius still is less than 100.0, and as long as the condition is true, the expression following it is executed. This expression is called the body of the loop. The loop condition is re-evaluated every time the body has been evaluated.
The example shows another noteworthy item: the body expression actually consists of a declaration and two expressions, separated by semicolons (;) and enclosed in braces, { and }}. Such an expression sequence between braces is called a compound expression. The type and value of such a compound expression are the type and value of the last expression within the compound. This value is not always used, like in this example.
Two other loop constructs are available, which basically are nothing more than a variation on the while loop theme.
The first variation is the do while loop. For example, the following code prints the numbers from 0 to 9 (inclusive or exclusive?).
int main Array argv { int counter; do { [[[stdio out] print counter] nl]; counter = counter + 1; } while (counter < 10); return 0; } |
The difference with the plain while loop is that the condition is evaluated after the body has been executed, i.e., the body is executed always at least once, whereas the body of the while loop can be skipped if the loop condition evaluates to FALSE before the body would be entered for the first round.
The third, and last, loop variation is the for loop. Like the while and do while loops, its syntactical origins stem from C. Here is a Fahrenheit to Celsius converter for degrees between -100 and 100, written down using a for loop.
int main Array argv { float f; for (f = -100.0; f <= 100.0; f++) [[[stdio out] print (f, " ", (f - 32.0) * 5.0 / 9.0)] nl]; return 0; } |
The for loop starts with three expressions, separated by semicolons. The first expression, in this case f = -100.0, is always executed. The second expression is the condition of the loop. If it evaluates to TRUE, the body is evaluated, followed by the evaluation of the third expression, f++, which increments f by 1. Then the condition is re-evaluated, and, if it is TRUE, the body, and so on.
As has been stated at the start of this chapter, everything in a method is an expression. Obviously then, loops also have a value. The value of a loop is the value of the body expression the last time it was executed. If the body is never executed, as can be the case with while and for loops, the value returned is the default value for the type.