| Previous | Table of Contents | Next |
$x = lollipop; $y = $x . $x; $y .= s\n; print $y;
#!/usr/bin/perl
$name = Will;
if ($name eq Will) { $name .= i; }
else { chop $name; }
if ($name ne Willi) { $name .= ly; }
elsif ($name eq Bill) { $name .= iard; }
else {$name .= am; }
Difficulty: Medium
Modify tickets3 to give the user a choice of two movies. It should ask which movie the user wants to see and then confirm that choice.
Loops execute the same statements over and over until some stop condition is satisfied (Figure 1-6). There are three commonly used looping constructs in Perl: while, foreach, and for. Well explore them (in that order) in Sessions 4, 5, and 6.
Figure 1-6 A while loop
The while Loop
while ( CONDITION ) { BODY } repeatedly executes the statements in BODY as long as CONDITION is True.
To understand loops better, lets play with a little algorithm that demonstrates loopish behavior:
Eventually, n will become 1 (we think; no one really knows for sure), but the number of steps will vary depending on the choice of n. 16 plummets to 1 quickly. 27 takes much longer. Experiment!
Lets rewrite these steps in a slightly different way:
The meander program (Listing 1-11) implements this algorithm using a while loop and an if...else statement.
Listing 1-11 meander: Using a while loop
#!/usr/bin/perl -w
print Please type an integer: ;
$num = <>;
chomp $num;
while ($num != 1) { # as long as $num isnt 1, do:
print $num ;
# If $num is even, halve it.
# Otherwise, multiply by 3 and add 1.
if (($num % 2) == 0) { $num /= 2; }
else { $num *= 3; $num++; }
}
print $num, \n; # print the last number (which must
# be 1, since the loop finished).
The while loop first tests to see whether $num is equal to 1. If it isnt, the statements inside the outer curly braces are executed. Then Perl returns to the top of the loop and checks $num again. This continues until $num finally does become 1, at which time the loop exits and Perl moves to the next statement in the program.
Heres meander in action:
% meander
RESULT: Please type an integer: 15
15 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
% meander
RESULT: Please type an integer: 20
20 10 5 16 8 4 2 1
% meander
RESULT: Please type an integer: 256
256 128 64 32 16 8 4 2 1
Suppose a loop has the condition ($num != 1. Then there should be some statement inside the loop that modifies $num. In fact, its downright critical! Otherwise, $num stays at whatever it was before entering the loop, the condition never becomes FALSE, and the loop never exits. See if you can figure out why these snippets of code loop forever.
#!/usr/bin/perl
$num = 0;
while ($num == 0) {
print num is $num.\n;
}
Bug! The loop variable $num never changes
#!/usr/bin/perl
$num = 1;
while ($num != 10) {
print $num is an odd number.\n;
$num += 2;
}
Bug! The stop condition is stepped over
The first loop never modifies $num at all. The second loop modifies $num, but not in a way that ever satisfies the stop condition because $num skips right from 9 to 11.
The % operator (pronounced mod, short for modulus) yields the remainder when the right side is divided by the left. Some examples:
% perl -e print 17 % 5
RESULT: 2
% perl -e print 6 % 3
RESULT: 0
% perl -e print 25 % 20
RESULT: 5
Because all even numbers are divisible by 2, the remainder will be 0, so ($num % 2) is 0 only if $num is even. (And should you ever find yourself needing a statement like $big = $big % 2, you can abbreviate it just like the assignment operators you saw earlier: $big %= 2.)
| Previous | Table of Contents | Next |