-
For Loops
The for loop has three expressions separated by semi-colons.
The first is the initialization statement. The second is the
condition which will be evaluated to either continue or
terminate the loop. The third is the reinitialization
expression.
|
for ($i = 1; $i < 10 ; $i++) {
print "\$i is $i\n";
}
exit;
|
When executed, the script above produces the following output:
|
[mark@iguana perl]
[mark@iguana perl] ./lab05_0.pl
$i is 1
$i is 2
$i is 3
$i is 4
$i is 5
$i is 6
$i is 7
$i is 8
$i is 9
[mark@iguana perl]
|
In the case above, the loop is initialized with the variable $i
set to a value of 1. The loop will continue until $i takes on a
value greater than 10. For each iteration of the loop $i will
be increased by a value of 1.
-
Do-while|until Loops
do while loops are identical to the while loops we used in the
previous lesson with the exception that the loop is run once
with no regard to the value of the while conditional. Any
subsequent iterations are dependent on the evaluation of the
condition.
|
@somevar = qw( alpha beta delta epsilon zeta eta iota gamma theta omega );
$counter = 1;
do {
print "$counter value is $somevar[$counter]\n";
$counter++;
} while ($counter <= 6);
exit;
|
When executed, the script above produces the following output:
|
[mark@iguana perl]
[mark@iguana perl] ./lab05_1.pl
1 value is beta
2 value is delta
3 value is epsilon
4 value is zeta
5 value is eta
6 value is iota
[mark@iguana perl]
|
do-until loops have the same behavior as do-while loops only the
loop will repeat only if the conditional evaluates as false.
The example below performs the same task as the do-while example
above.
|
@somevar = qw( alpha beta delta epsilon zeta eta iota gamma theta omega );
$counter = 1;
do {
print "$counter value is $somevar[$counter]\n";
$counter++;
} until ($counter > 6);
exit;
|
When executed, the script above produces the same output as the
do-while loop:
|
[mark@iguana perl]
[mark@iguana perl] ./lab05_2.pl
1 value is beta
2 value is delta
3 value is epsilon
4 value is zeta
5 value is eta
6 value is iota
[mark@iguana perl]
|
-
Loop Control
Sometimes you will want to interrupt the flow of a loop based on
some expression other than the condition used to define the flow
of the loop. There are three functions that can be used to
alter the loop: next, last
and redo
.
next will stop the processing of any statements remaining in the
block and allow the loop to continue as though the entire block
has been executed.
|
for ($moo = 7; $moo >= 0; $moo--) {
next if( $moo == 4 );
print "The number is $moo\n";
}
exit;
|
When executed, the script above produces the following output.
Notice that there is no output for the value of 4.
|
[mark@iguana perl] ./lab05_3.pl
The number is 7
The number is 6
The number is 5
The number is 3
The number is 2
The number is 1
The number is 0
[mark@iguana perl]
|
last
stops the loop and any remaining statements in
the block are not executed.
|
for ($moo = 7; $moo >= 0; $moo--) {
last if( $moo == 4 );
print "The number is $moo\n";
}
exit;
|
When executed, the script above produces the following output:
|
[mark@iguana perl] ./lab05_4.pl
The number is 7
The number is 6
The number is 5
[mark@iguana perl]
|
redo
causes the loop to restart but does not
reinitialize the loop.
|
for ($moo = 7; $moo >= 0; $moo--) {
$output .= $moo;
print "The number is $output\n";
redo if( $output == 7654 );
}
exit;
|
When executed, the script above produces the following output:
|
[mark@iguana perl] ./lab05_5.pl
The number is 7
The number is 76
The number is 765
The number is 7654
The number is 76544
The number is 765443
The number is 7654432
The number is 76544321
The number is 765443210
[mark@iguana perl]
|
-
Addition
The addition operator in perl is the +
character.
Some examples:
|
$x = 4;
$y = 3;
$var = $x + $y;
print "Value of \$var is $var\n";
$x += $y;
print "Value of \$x is $x\n";
$x++;
print "Value of \$x is now $x\n";
exit;
|
When executed, the script above produces the following output:
|
[mark@iguana perl] ./lab05_6.pl
Value of $var is 7
Value of $x is 7
Value of $x is now 8
[mark@iguana perl]
|
-
Subtraction
The subtraction operator in perl is the -
character.
Some examples:
|
$x = 10 - 4;
$y = 3;
$var = $x - $y;
print "Value of \$var is $var\n";
$x = 10 - 4;
$x -= $y;
print "Value of \$x is $x\n";
$x--;
print "Value of \$x is now $x\n";
exit;
|
When executed, the script above produces the following output:
|
[mark@iguana perl] ./lab05_7.pl
Value of $var is 3
Value of $x is 3
Value of $x is now 2
[mark@iguana perl]
|
-
Multiplication
The multiplication operator in perl is the *
character.
Some examples:
|
$x = 10 * 4;
$y = 3;
$var = $x * $y;
print "Value of \$var is $var\n";
$x *= $y;
print "Value of \$x is $x\n";
exit;
|
When executed, the script above produces the following output:
|
[mark@iguana perl] ./lab05_8.pl
Value of $var is 120
Value of $x is 120
[mark@iguana perl]
|
-
Division
The division operator in perl is the /
character. Normal division will return the value of the
operation with any fractional parts represented as decimal
values. To get the remainder of a division operation the
%
operator may be used.
Some examples:
|
$x = 100 / 4; $y = 3;
$var = $x / $y;
print "Value of \$var is $var\n";
$rem = $x % $y;
print "Value of \$rem is $rem\n";
$x /= $y;
print "Value of \$x is $x\n";
print "Formatted as ".sprintf("%0.2f", $x)."\n";
exit;
|
When executed, the script above produces the following output:
|
[mark@iguana perl] ./lab05_9.pl
Value of $var is 8.33333333333333
Value of $rem is 1
Value of $x is 8.33333333333333
Formatted as 8.33
[mark@iguana perl]
|
-
Exponents
To get an exponent operation, the **
operator is
used. For example, to get the value of 72, you
would use the statement:
print 7**2."\n";
Some examples:
|
$x = 7**2; $y = .5;
print "Value of \$x is $x\n";
$var = $x ** $y;
print "Value of \$var is $var\n";
exit;
|
When executed, the script above produces the following output:
|
[mark@iguana perl] ./lab05_10.pl
Value of $x is 49
Value of $var is 7
[mark@iguana perl]
|
-
Other Math Functions
Some examples:
|
$x = 2.5;
$z = log($x); print "$z is the log of $x\n";
$z = sin($x); print "$z is the sine of $x\n";
$z = int($x); print "$z is the integer value of $x\n";
$x *= -1;
$z = abs($x); print "$z is the absolute value of $x\n";
$someval = int((sin($x) - 1)/int(abs($z**3)) + 3);
print "Someval is $someval\n";
exit;
|
When executed, the script above produces the following output:
|
[mark@iguana perl] ./lab05_11.pl
0.916290731874155 is the log of 2.5
0.598472144103957 is the sine of 2.5
2 is the integer value of 2.5
2.5 is the absolute value of -2.5
Someval is 2
[mark@iguana perl]
|
Additional math functions can be used within perl by using the
perl Math modules:
Math::BigFloat (3) - Arbitrary size floating point math package
Math::BigInt (3) - Arbitrary size integer math package
Math::BigInt::Calc (3) - Pure Perl module to support Math::BigInt
Math::BigRat (3) - arbitrarily big rationals
Math::Complex (3) - complex numbers and associated mathematical functions
Math::Trig (3) - trigonometric functions
The use of modules will be covered in a later lesson.