Download - PERL Unit 4 Control Flow
-
8/8/2019 PERL Unit 4 Control Flow
1/19
Unit 4Flow Control
VSRivera
IBM Learning Services
Worldwide Certified Material
-
8/8/2019 PERL Unit 4 Control Flow
2/19
Objectives After completing this unit, students
should be able to use: Statement blocks
if/else statement
elsifbranch
unless variation
while and until statements
for statement
foreach statement
-
8/8/2019 PERL Unit 4 Control Flow
3/19
Statement Block It is a list of statements surrounded
by curlybraces
Statements are executed sequentially Syntax
{ first_stmt;
second_stmt;
. . .
nth statement ; }
-
8/8/2019 PERL Unit 4 Control Flow
4/19
Statement Block Statement block acts like a single
statement and can replace a single
statement in most places. As a special concession to writers of
short programs, the last ; before theclosing } is optional.
-
8/8/2019 PERL Unit 4 Control Flow
5/19
if/else Construct The basic decision-making control
structure
Syntaxif (test_expr) { stmt/s; }
else { stmt/s ; }
The body of the if/else are defined as
a statement block, the braces arerequired even if there is only onestatement.
-
8/8/2019 PERL Unit 4 Control Flow
6/19
elsif Construct Provides for multiple test conditions
Syntax
if (test_expr) { stmt/s; }elsif (test_expr) { stmt/s; }
else { stmt/s; }
You can have as many
elsif asrequired
Else clause is still optional
-
8/8/2019 PERL Unit 4 Control Flow
7/19
unless Construct When you want to execute only the
else part of if/else
Execute the statement block is thetest is false
Syntax
unless (test_expr) { stmt/s; }
Example
if ($month 12} { . . . }
-
8/8/2019 PERL Unit 4 Control Flow
8/19
unless Construct
if (! ($month > 0 && $month < 13} { . . . }
Use unless() to execute statementswhen a conditions is false
This saves having to invert thecondition
-
8/8/2019 PERL Unit 4 Control Flow
9/19
while Construct Execute a statement block while a
condition is true
Syntaxwhile (test_expr) { stmt/s;}
the loop may execute endlessly if the
condition does not change
-
8/8/2019 PERL Unit 4 Control Flow
10/19
do/while Construct Test the condition after the first
execution of the block
Syntaxdo { stmt/s ; } while (test_expr);
-
8/8/2019 PERL Unit 4 Control Flow
11/19
Default Variable : $_
Special global variable
Used by many functions if no other
variable specified: print;
chop;
chomp;
$_ is used if no parameter isprovided.
-
8/8/2019 PERL Unit 4 Control Flow
12/19
Default Variable : $_
$_ corresponds to the word it, thecurrent subject, except that when $_
is used, it is often not visible.
-
8/8/2019 PERL Unit 4 Control Flow
13/19
while() shourtcut using $_ When the input operator is the
only condition in a while()
The input value is assigned to$_ Example
while () {
chomp;print; }
-
8/8/2019 PERL Unit 4 Control Flow
14/19
while() shourtcut using $_ Is the same as
while (defined($_=))
{ chomp $_;print $_; }
-
8/8/2019 PERL Unit 4 Control Flow
15/19
until Construct Variation of the while statement
Execute a statement block until a
condition is true Syntx
until (test_expr) { stmt/s; }
do { stmt/s; } until (test_expr);
-
8/8/2019 PERL Unit 4 Control Flow
16/19
until Construct The statement block is executed while
the test condition is false
The first form may not execute at allif the condition is initially true
until is less common than while(), butuseful when the condition is reversed.
The do/until version corresponds todo/while
-
8/8/2019 PERL Unit 4 Control Flow
17/19
for Construct A loop with initialization, test and
increment expressions
Syntaxfor (init_expr; test_expr; incr_expr)
{ stmt/s; }
-
8/8/2019 PERL Unit 4 Control Flow
18/19
foreach Construct Executes a block for each element of
a list of values
Sets a loop variable to each value inturn
Syntax
foreach $item (list) {stmt/s;}
-
8/8/2019 PERL Unit 4 Control Flow
19/19
foreach Construct Example
foreach $flag(red, white, blue)
{ print $flag; } If omitted, the loop variable default
to $_
foreach (red, white, blue){ print; }