Control Structures if Conditional Web Programming 1 Review

Control Structures: if Conditional Web Programming 1

Review n Double quotes vs. Single-quotes ► Variables inside double quotes are replaced by their values. • e. g. “Hello, $name” == Hello, Jim (when $name == “Jim”) ‘Hello, $name’ == Hello, $name ► Escape character (i. e. ) has special meaning inside double quotes • e. g. “Hello, Ujim” == Hello, JIM ‘Hello, Ujim’ == Hello, Ujim n Interchangeability of Strings and Numeric Values ► String can be used as numbers when appropriate, and vice versa • e. g. $a= “ 25”; $sum= $a+5; • e. g. $number = <STDIN>; chop $number; $sum= $number+5; ► String used in integer context inappropriately will produce unexpected results. • $b = “five” + 5; • $b = “ 15 k 50” + 5; • $b = “k 1550” + 5; n ($b == 5) ($b == 20) ($b == 5) Initial value of Scalar Variable ► ► Perl scalar variables have an initial value of the null string (“”) Null string is converted to 0 in integer context Web Programming 2

Control Structures: Overview n What are Control Structures? ► Program statements that control the program flow à n i. e. controls which statement to execute next Perl Control Structures ► if conditional • Execution of statements depend on current condition of the expression • if (expression) { statements } à ► e. g. if ($count<5) { print “count is $countn”; } while loop • Statements are executed while expression is true • Syntax: while (expression) { statements } à ► e. g. while ($i<5) { print “count is $in”; $i++; } for loop • Statements repeats for a given number of times • Syntax: for (expression) { statements } à e. g. for ($i=0; $i<5; $i++) { print “count is $in”; } Web Programming 3

Conditional Expressions n Conditional Expression is a key component of Control Structures ► ► The “value” of the conditional expression determines the program flow Syntax: KEYWORD (expression) { statements } • e. g. if ($count<5) { print “count is $countn”; } n Conditional Expressions can be any Perl expressions ► Constant or Variable • e. g. (1), (0), (“true”), ($a) ► Using Comparison Operators • ==, <, >, eq, lt, gt, etc. • test for single condition, e. g. ($a > $b) ► Using Logical Operators • &&, ||, !, etc. • test for multiple conditions, e. g. ($a<0 || $a>9) n Evaluation of Conditional Expression ► True if the value of the expression is non-zero. • e. g. (1), (“false”), (“ 00”), (“ ”) ► False if the value of the expression is zero (or null). • e. g. (0), (“”), (“ 0”) Web Programming 4

Comparison Operators n Comparison Operators (CO) are used in Conditional Expressions, which are used in Control Structures. ► ► n Numeric Comparison Operators ► n Control Structure = KEWORD (conditional expression) { statements } Conditional Expression = (value 1 CO value 2) ==, !=, >, >=, <, <= String Comparison Operators ► ► eq, ne, gt, ge, lt, le Strings are compared from left to right in the alphabetical order • “aa” is less than “ab” n Alphabetical order ► null < blank < !”#$%&’()*+, -. / < numbers < : ; <=>? @ < A-Z < []^-`< a-z < {|}~ Web Programming 5

Comparison Operators: Pitfalls n String vs. Integer comparison ► ► When comparing numbers, comparison operator drives the evaluation of an expression numeric comparison for numeric comparison operator • TRUE: (“ 123” > “ 45”), (123 > 45) ► alphabetical comparison for string comparison operator • FALSE: (“ 123” gt “ 45”), (123 gt 45) Example script Web Programming 6

Logical Operators n Logical Operators, also used in Conditional Expressions, can test for multiple conditions ► n e. g. ( ($a<0) || ($a>9) ) Logical AND ► && (and) • (A && B) : TRUE when both A and B are true n Logical OR ► || (or) • (A || B) : TRUE when either A or B are true n Logical NOT ► ! (not) • (!A) : TRUE when A is false *Note: FALSE == 0 or null, TRUE == NOT(0 or null) Example script Web Programming 7

if Conditional n Syntax ► if (condexpr 1) { statements 1 } elsif (condexpr 2) { statements 2 } else { statements 3 } • statements are executed if condexpr is true n Single-Line Conditional Statements ► When statement block consists of only one statement, • Syntax: statement KEYWORD (expression) • e. g. print “You cannot buy beern” if ($age<21); Example script Web Programming 8
- Slides: 8