8
SCALAR EXPRESSIONS Scalar data items are combined into expressions using operators. $c=12; $d=3+($c); 1) ARITHMETIC OPERATORS Perl provides usual arithmetic operator including auto-increment and auto-decrement. Operator Example Result Definition + 7 + 7 = 14 Addition - 7 - 7 = 0 Subtraction * 7 * 7 = 49 Multiplicat ion / 7 / 7 = 1 Division ** 7 ** 7 = 823543 Exponents % 7 % 7 = 0 Modulus $c=17; $d=++$c; //increment then assign $c=12; $d=$c ++; //assign then increment Binary arithmetic operation: $a+=3; $a=a+3;

Unit 1-scalar expressions and control structures

Embed Size (px)

Citation preview

Page 1: Unit 1-scalar expressions and control structures

SCALAR EXPRESSIONS Scalar data items are combined into expressions using operators.

$c=12;$d=3+($c);

1) ARITHMETIC OPERATORSPerl provides usual arithmetic operator including auto-increment and auto-decrement.Operator Example Result Definition+ 7 + 7 = 14 Addition- 7 - 7 = 0 Subtraction* 7 * 7 = 49 Multiplication/ 7 / 7 = 1 Division** 7 ** 7 = 823543 Exponents% 7 % 7 = 0 Modulus

$c=17;$d=++$c; //increment then assign$c=12;$d=$c ++; //assign then incrementBinary arithmetic operation:

$a+=3;$a=a+3;

Page 2: Unit 1-scalar expressions and control structures

2)String OperatorsIn Perl most of the processing is done by using built-in functions and regular expressions.

Perl uses period (.) for concatenation of strings The other string operator is (x), used to replicate strings.

$a=“hello” x 3$a=“hellohellohello”

$foo.= “ ” Arithmetic Operators used in String Context.1. Auto-increment

This operator can be applied to a variable.If a variable is assigned with string of letters and digits then auto increment operation is applied on the string from rightmost character.eg: $a=‘all12’;print ++$a;result: bmm23

2. Unary minusIf this is applied to string which starts with a plus or minus character, then it returns the same string with opposite sign.eg. $name=“sia”-$name=“-sia”

Page 3: Unit 1-scalar expressions and control structures

3) Comparison operatorValues of comparison is returned in numbers.1--- if true and (“ ”) – if false

These are classified into two kinds. One for numbers and other for strings.

Numbers: ==,!=,<,>,<=,>=,<=>(comparison operator) Strings: eq,ne,lt,gt,le,ge,cmp 4)Logical operator

not ---- !and ---- &&or ---- ||

print “OK\n” if $a<10 and $b<12; 5) Conditional expressions

it is the one whose value is chosen from one of the alternatives at runtime depending on the outcome of test.

test? True_exp:false_expeg: $a=($a<0)?0:$a;

Page 4: Unit 1-scalar expressions and control structures

CONTROL STRUCTURES A control structure is a block of programming that analyzes variables and chooses a

direction in which to go based on given parameters. The term flow control details the direction the program takes (which way program control "flows").

Blocks: It is a sequence of one or more statements enclosed in curly braces.eg: {

$positive=1;$negative=-1;}

Conditions: They make use of relational operators. It is a Perl expression which is evaluated in Boolean context.

If it evaluates to --- 0 or (“ “) --- condition is false, else it is treated as true. $total > 50 and $total <100 A condition can be negated using ! Operator, we can specify that as !($total > 50 and $total<100)

Page 5: Unit 1-scalar expressions and control structures

CONDITIONAL EXPRESSIONS Conditional Expressions should be in brackets. If-then-else statement:

if($total> 0){print “$total\n”} else {

print “wrong total ! \n”}

If-elsif statementif($total> 70) { $grade=“A”;} elsif($total > 56){ $grade =“B”;} else{$grade=“C”); }

Alternative to if-then-else statements are ‘conditional expression’ and using ‘or’ operator.

In perl a single statement can be followed by a conditional modifier.print “OK\n” if $volts>=1.5;

Page 6: Unit 1-scalar expressions and control structures

REPETITION Testing loops and counting loops can be used for repetition mechanisms. Testing loops: while($a!=$b){

if($a > $b){$a=$a-$b;}else {$b=$b-$a;}}

An until loop statement in Perl programming language repeatedly executes a target statement as long as a given condition is false.

Syntax The syntax of an until loop in Perl programming language is − until(condition) { statement(s); } Here statement(s) may be a single statement or a block of statements. The condition may be any expression. The loop iterates until the condition becomes true. When the condition becomes true, the program control passes to the line immediately

following the loop. $a+=2 while $a < $b; (and) $a+=2 until $a < $b; Although condition is specified after the written statement, it is evaluated before the

statement executed.

Page 7: Unit 1-scalar expressions and control structures

Do loop:It is built-in function rather than a syntactic construct.Overhere the condition is tested after the execution of the block, so the block is executed at least once.

do{...

}while $a!=$b; Counting Loops: They use same syntax as c

for($i=1;$i<=10;$i++){$i_square=$i*$i;$i_cube=$i**3;print “ $i\t$i_square\t$i_cube\n”;

} foreach $i (1..10) {

$i_square=$i*$i; $i_cube=$i**3; print “ $i\t$i_square\t$i_cube\n”; }

Page 8: Unit 1-scalar expressions and control structures

LOOP REFINEMENTS Perl provides three loop commands : last , next and redo . The last and next command

are similar to break and continue statements in c language. last breaks out of the loop and next forces the next iteration of a loop. Ex: to terminate input processing if a line contains ‘quit’ is read we write

while <STDIN> {

last if /quit/;

...

} The redo command repeats the current iteration from the beginning.

Output:

3 4 5 6 7