Perl - Part 11
[home] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20] [21] [22] [23] [24] [25]
Repitition
There is a saying, worth remembering, that "Computers don't get happy and they don't get sad; they just run programs". This is true, and is one of the reasons that computers are so useful in the first place; they are able to run a tedious program forever without getting bored. Therefore we have mechanisms that allow us to write programs that carry out an action more than once.
The WHILE Construct
In Perl there are:
- while
- do..while
- for
General form of the while statement
The general form of the while statement, without specific variables is:
initialize control_variable;
while (condition remains TRUE based on value of control_variable)
{
Carry out all actions enclosed in the block defined by the brackets;
Progress the loop: the while test eventually yields FALSE and the loop ends;
}
Task 010
This is a sample program. It reads in numbers, printing a running total of the numbers entered each time the loop goes around (an 'iteration'). When the value entered is 0, the loop stops.

The source code for this program is here. Make sure you understand it before moving on.
#!/usr/bin/perl
# Identify the author and present a few blank lines
print "\n\nBy Fachtna Roe:\n\n";
#Initialize the totalling valiable to 0
$running_total = 0;
# Initialise the controlling variable to any value other than 0
$entered = 1;
while ($entered != 0)
{
print "Please enter a number, or '0' to stop: ";
$entered = <STDIN>;
chop $entered;
$running_total = $running_total + $entered;
print "The total so far is: $running_total.\n";
}
print "\nProgram ends!\n";
Task 011
Modify the program above to print the square of the number entered as each iteration occurs. Do not remove any existing functions.
Task 012
Further modify the program to print, at the end, how many numbers (excluding the 0) were entered.
Numeric comparison
Notice how when we compare the variable $entered with 0 that we use the symbol != meaning 'not equal to'. We could have used ne and Perl would have forgiven us; but technically if would be incorrect to do so. Perl, unusually, has a different set of comparison operators for numbers than it has for text. Mostly the text operators seem to work for numeric comparison but be aware that both sets exist. These are the numeric comparators, which should make sense at a glance:
- <
- <=
- ==
- !=
- >=
- >
Task 014
This program is one which might normally be written with a for loop. If you've done programming previously and know what that is, that's good for you. Otherwise, please erase the last sentence from your mind. This program performs numeric calculations in a sequential fashion. It shows again the power of the computer to perform a repetitive task, but this time with values that we more directly control.

Sample output from the program is shown here:

Task 015
The last program counts upwards in steps of 1 at a time. Modify the task to ask from the user the start value, the end value and the step count by which the program should progress, and then make the program move forward by that step count each time. To ensure that a suitable (>0) value is entered use an IF statement (possible with a matching ELSE) to ensure that a positive value greater than zero is entered; if an unsuitable value is entered just use a a step count of 1.
- For extra kudos, you can use a WHILE construct instead of an IF construct, forcing the user to stay at the step count entry part of the program until a suitable value is entered. This is optional
- For real kudos, use a similar WHILE mechanism to ensure that the starting value is lower than the ending value. This also is optional.
More anon...
[home] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20] [21] [22] [23] [24] [25]