Download presentation
Presentation is loading. Please wait.
Published byJanis Carr Modified over 8 years ago
1
Perl Chapter 3 Conditional statements
2
Control Expressions Control expressions – interpreted as T/F (evaluated as strings or numbers) – simple, relational, compound Simple control expressions – arithmetic or string expressions – if value is a string empty string and “0” interpreted as false, otherwise true – if value is a number it’s true unless its value is zero – any undefined value is false (because undef number is 0 and undef string is empty
3
Relational expressions – includes a single relation operator (e.g. >) – Perl includes 2 sets of six relational ops for numbeic and string operands (Table 3.1, p36) – relational operators coerce operands to proper type – strings known to be numbers may be compared with numeric ops “27” > “3” is true string op “27” gt “3” is false – More examples “fruit” eq 7 “fruit” eq “7” “boy” == “girl” 0 == 0
4
Precedence equal operators have lower precedence – e.g. == != eq, ne all relational ops have precedence lower than arithmetic ops – ex. 0.03 * $a < $limit * first
5
Compound Expressions Consist of scalar variables, scalar literals, relational expressions and BOOLEAN operators Two sets of Boolean operators – &&, !!, ! lower precedence than relational ops short circuit – and, or, not perform same, except have LOWER precedence than that of any other Perl operators more readable – See Table 3.2 precedence and associativity
6
Control Statements: Selection if if ($a > $b) { print “\$a is greater than \$b \n”; $max = $a; } if ($total) { 0 interpreted as false $average = $sum / $total; print “The average is $average \n”; } else { print “average cannot be computed. \n”;
7
unless – doesn’t require reverse logic with if unless (($count < $limit) or $no_limit) { print “I cannot go on!\n” } if (($count >= $limit) and ($nolimit == 0))
8
Nested if statement if possible, make nested if in the else clause (linear nesting) elsif - example pg. 40 No switch– use nested ifs (later section 3.4)
9
Conditional expression boolean_expr ? then_expr : else_expr can be used in the assignment statement $average = ($number != 0) ? $sum/$number:0; (pg. 41 example)
10
Iteration: while, until, and for All pretest loops while (control expr) { #loop body } until ($sum > 1000) { $sum += ; … } (stops when condition becomes true) for (init expr; control expr; increment expr) { #loop body } Examples: gpa1.pl and gpa2.plgpa1.plgpa2.pl
11
$sum = 0; for ($count = 0; $count <10; $count ++) { $value = ; $sum += $value; } all three expressions are optional (;’s must occur) if control expr omitted interpreted as true
12
all three expressions may be multiple expressions (or statements) separated by,’s (comma is an operator) value returned by a list of comma separated expressions is the value of the last expression for ($x = 0, $y =n; $x <n; $x++, $y--) { } assignment statements as control expressions for ($sum=0; $value = ; $sum += $value) { } – when gets to eof, it returns empty string, which represents false variables that appear in loop control expressions may occur in loop body (may even have their value changed)
13
Ways out of a block: last, next, redo may be used in any block most commonly used within loops abnormal statement flow control - UGLY!!!! next operator (w/out an operand) transfers control to the end of the block – if block is a while control goes back to control expression of loop
14
last operator in a loop body causes loop to be immediately terminated control is transferred to the first statement after loop body redo operator (appearing in loop body) causes control to transfer to top of loop body (not the test ) … careful, infinite loop – iteration is redone last, next, redo often used in conjunction with a labeled block
15
LOOP: indent followed by a colon while (…) { … } In nested loop these operators only apply to loop in which they appear Sometimes we would like to perform these ops on an enclosing loop or block. OUTER: for ( …) { … last OUTER; } switch example: gpa3.plgpa3.pl
16
last and the fake switch last operator when in loop body causes loop to terminate (control transferred to 1 st statement after loop body) DON’T DO UGLY STUFF! Do for a labeled block, specifically to fake a switch
17
SWITCH: { if ($rank == 1) { $freshman++; last SWITCH; } if ($rank == 2) { $sophomore++; last SWITCH; } if ($rank == 3) { $junior++; last SWITCH; } if ($rank == 4) { $senior++; last SWITCH; } print “ERROR –not a legal class rank. \n”; }
18
die takes a list of parameters, catenates them, prints to STDERR (output usually same as STDOUT), terminates program execution die “ERROR –not a legal class rank.”; – example in gpa3.pl line 27 catenates program name, line number of die and \n If you include \n in the string, info not provided System error may be stored in $!, can include die “ERROR –in SWITCH $!”; no error message exit 0;
19
3.9 Debugger READ IT!
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.