Presentation is loading. Please wait.

Presentation is loading. Please wait.

CPTG286K Programming - Perl Chapter 4: Control Structures.

Similar presentations


Presentation on theme: "CPTG286K Programming - Perl Chapter 4: Control Structures."— Presentation transcript:

1 CPTG286K Programming - Perl Chapter 4: Control Structures

2 Statement Blocks A statement block is a sequence of statements enclosed in matching curly braces Block statements are accepted in place of any single statement The final semicolon on the last statement in a block is optional

3 If statements Like most programming languages, if statements in PERL have a true statement part, optionally a false statement part, and elsif statement parts Any expression that converts or computes to “0” or “” is false; the expression is true otherwise

4 Unless statements The unless statement produces a control flow equivalent to the logical complement of the original expression used by the if statement: if ($a < 18)# original expression { print “You’re not old enough to vote\n”; } unless ($a = 18) { print “You’re an old man\n”; }

5 While/Until statements Like all programming languages, while loops contain a control expression and a statement block PERL executes statements in the loop while the control expression is true The until statement produces a control flow equivalent to the logical complement of the original expression used by the while statement

6 While and Until comparisons while ($a > 0)# original test expression { print $a;# display $a $a--;# decrement $a } # DANGER! Endless loop if initially $a = 0 until ($a > 0)# same as while ($a <= 0) { print $a;# display $a $a--;# decrement $a }

7 Do while/until statements The do statement guarantees that the statements within the loop execute at least once $stops = 0;# initialize stops do { $stops++;# increment $stops print “Next stop? “;# print prompt chomp ($location = );# get $location } until $stops > 5 || $location eq ‘home’; # or while $stops <= 5 || $location eq ‘home’;

8 For statements Similar to C and Java, the PERL for loop contains an initial expression, a test expression, and an in/decrement expression for ($i = 1; $i <= 10; $i++) { print “$i ”; }

9 Foreach statements A foreach statement assigns each element of a list to a scalar variable and executes a statement block @a = (1,2,3,4,5);# initialize @a foreach $b (reverse @a)# (reverse @a) is assigned {# to each scalar variable $b print $b;# and printed }

10 The $_ variable $_ is a local temporary variable that can be used instead of a scalar variable @a = (1,2,3,4,5);# initialize @a foreach (reverse @a)# (reverse @a) is assigned {# to each scalar variable $_ print;# and printed }# can also use print “$_ “;

11 Iterating over real variables When iterating over real variables (rather than iterating over a function generated list), the iteration variable becomes an alias for each real variable. Changes to the iteration variable affects each real variable: @a = (3,5,7,9); foreach $one (@a) { $one *= 3; }# changes to $one also affects @a # @a is now (9,15,21,27)


Download ppt "CPTG286K Programming - Perl Chapter 4: Control Structures."

Similar presentations


Ads by Google