1.4.1 The while construct
In a programming language, usually there are several ways to achieve the same task just like as humans, we can say the same thing in many ways. In this section, we look at two different ways of performing iteration, i.e., performing the same computation several times with possibly different inputs in every iteration.
A program that produces the desired output follows.
Program 1.4
#!/usr/bin/perl
#file farenheit.pl
use strict vars;
my ($lower, $upper, $step, $fahrenheit, $celsius);
#print Fahrenheit-Celsius table for 0, 20, ..., 300 degrees Fahrenheit
$lower = 0; #lower limit of temperature table
$upper = 300; #upper limit
$step = 20; #step size
$fahrenheit = $lower;
#loop through the Fahrenheit values
while ($fahrenheit <= $upper){
$celsius = 5/9 * ($fahrenheit - 32);
printf "%4.0f %6.1f\n", $fahrenheit, $celsius;
$fahrenheit = $fahrenheit + $step;
}
The program initializes the lower and upper Fahrenheit limits of the table we want to print. Next, it initializes the value by which a Fahrenheit degree is increased in each iteration of the loop to be 20 degrees.
We use a while loop in the program. The expression inside parentheses following the while keyword gives the condition that must be satisfied for the statements inside the loop to be executed. The condition simply states that the value of
$fahrenheit must be less than or equal to the value of $upper for the body of the while loop to be executed. The body of the while loop contains all the statements enclosed within { and }. The body is an example of a block. Since the entry condition is satisfied, the loop is executed at least once.
In the body of the loop, there are three statements. The first statement computes the Celsius temperature for the Fahrenheit temperature given as the value of the scalar variable $fahrenheit using the formula we discussed earlier. The value of the scalar $celsius is set to the value computed.
The next statement allows us to print formatted output. That is why it is called
printf.
printf "%4.0f %6.1f\n", $fahrenheit, $celsius;
The first argument to printf is a doubly-quoted string that tells Perl what to print and how. It contains formatting directives that start with %. There can be any number of arguments following the first argument. Here we have two such arguments: $fahrenheit and $celsius. The first formatting directive inside the first argument is %4.0f. The f at the end tells us that we are printing a floating point number using a decimal point. There are 4 digits before the decimal point and none after the decimal point. The value of $fahrenheit - the second argument to the printf command— is printed according to the first formatting directive. The value of the third argument to printf is printed according the %6.1f directive. That is, $celsius is printed in a floating point format using a decimal point. There are up to six digits before the decimal point, and one digit after the decimal point.
The last statement in the loop increments the value of $fahrenheit by the value of $step. Control of the program is then sent back to the top of the while loop. The entry condition for the loop is evaluated once again. If the entry condition is satisfied, the loop is entered again and its body statements are performed. If the entry condition is not satisfied, the loop is not entered another time and the program execution ends.
