Presentation is loading. Please wait.

Presentation is loading. Please wait.

Control Structures: if Conditional

Similar presentations


Presentation on theme: "Control Structures: if Conditional"— Presentation transcript:

1 Control Structures: if Conditional
Web Programming

2 Review 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 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 == 5) $b = “15k50” + 5; ($b == 20) $b = “k1550” + 5; ($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

3 Control Structures: Overview
What are Control Structures? Program statements that control the program flow 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 $count\n”; } while loop Statements are executed while expression is true Syntax: while (expression) { statements } e.g. while ($i<5) { print “count is $i\n”; $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 $i\n”; } Web Programming

4 Conditional Expressions
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 $count\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) 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

5 Comparison Operators Comparison Operators (CO) are used in Conditional Expressions, which are used in Control Structures. Control Structure = KEWORD (conditional expression) { statements } Conditional Expression = (value1 CO value2) Numeric Comparison Operators ==, !=, >, >=, <, <= 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” Alphabetical order  null < blank < !”#$%&’()*+,-./ < numbers < < A-Z < [\]^-`< a-z < {|}~ Web Programming

6 Comparison Operators: Pitfalls
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

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

8 if Conditional Syntax Single-Line Conditional Statements
if (condexpr1) { statements1 } elsif (condexpr2) { statements2 } else { statements3 } statements are executed if condexpr is true Single-Line Conditional Statements When statement block consists of only one statement, Syntax: statement KEYWORD (expression) e.g. print “You cannot buy beer\n” if ($age<21);  Example script Web Programming


Download ppt "Control Structures: if Conditional"

Similar presentations


Ads by Google