Perl - Decision Making and Looping
In a Nutshell - CIW Course Section 2, Part B1, Chapter 4
Decision Making and Looping
A program can be likened to a journey. It could be simple, a straight road from start to end. It could be more complicated and may require changes to the route depending upon circumstance like the weather, traffic, or road works. Many decisions to be made before the optimum route can be decided upon.
If <expr> Statement
The most simple of decision making tests. If <expr> evaluates to true then the code contained in 'statement' will be executed
if ($age >= 65)
{
print ("You ought to be retired!");
}
Else Branches
The 'else' branch is a logical extension of the 'if' statement. We have just seen how to execute a section of code when an expression evaluates to true. The 'else' statement allows us to specify an alternate section of code to be executed when the expression does not evaluate to true.
if ($age >= 65)
{
print ("You ought to be retired!");
}
else
{
print ("Still working eh!");
}
ElsIf Branches
The 'elsif' statement allows for testing alternative, or multiple, expressions to control the program branching. Don't confuse it with 'elseif' which is used in other languages.
if ($score >= 90)
{
print ("Excellent! You have achieved an A grade.");
}
elsif ($score >= 70)
{
print ("Well done! You have achieved a B grade.");
}
else
{
print ("Need to work harder next time.");
}
There is no limit to the number of 'elsif' statement that can be included to allow multiple branch points. The above example could have had an addition test at 50% to show a pass or fail result.
While Loops
The 'while' expression allows a block of code to be executed, repeatedly, for as long as expression evaluates to true. This requires careful consideration as expression must evaluate to false, at some point, to allow the loop to end. There are ways to break out of the loop, but more on this later.
$a = 0;
while ($a < 5)
{
$a++;
print ("$a\n");
}
Do While Loops
Where the 'while' loop evaluates the expression at the start of the loop, the 'do while' loop evaluates the expression at the end of the loop.
$a = 0;
do
{
$a++;
print ("$a\n");
} while ($a < 5)
The above example accomplishes the same result as the previous one, but there are times when it is much better to evaluate the expression at the end of the loop.
For Loops
The 'for' loop permits the execution of a code block for a specific number of iterations. The basic syntax of the command is:
for (<initialisation>; <condition>; <modifier>)
The loop uses a variable as a counter and the three, above, expressions include: the initialisation to set the variable, the condition to test the variable value which is effectively a 'loop while' test, and the modifier where the variable value is altered, usually incremented.
for ($a=1; $a<6; $a++)
{
print ("$a\n");
}
Loop Control Commands
In order to take advantage of loop control commands, it is necessary to name, or label, the loop.
| Loop Control Commands | |
|---|---|
| Command | Description |
| last | Exits the loop |
| next | Jumps execution to the start of the loop at the next iteration |
| redo | Jumps execution to the start of the loop but skips the condition evaluation |
These are useful for breaking out of a loop, or restarting the loop under specific conditions.
ALOOP: for ($a=0; $a<10; $a++)
{
if ($a == 6)
{
last ALOOP;
}
print ($a\n");
}
I/O Redirection
When a Perl script is run, it's output goes, by default, to the screen. I/O redirection can alter this and instruct the interpreter to send the output to a file or other device.
perl anyscript.pl > yourfile.txt
The above syntax will direct the output from the script to the file "yourfile.txt".
perl anyscript.pl >> yourfile.txt
The above syntax will also direct the output from the script to the file "yourfile.txt", but in this case the output is appended to the file.

