SlideShare a Scribd company logo
2
Most read
3
Most read
4
Most read
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 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 increment
Binary arithmetic operation:
$a+=3;
$a=a+3;
 2)String Operators
In 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 minus
If 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”
 3) Comparison operator
Values 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 “OKn” 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_exp
eg: $a=($a<0)?0:$a;
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)
CONDITIONAL EXPRESSIONS
 Conditional Expressions should be in brackets.
 If-then-else statement:
if($total> 0){
print “$totaln”
} else {
print “wrong total ! n”
}
 If-elsif statement
if($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 “OKn” if $volts>=1.5;
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.
 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 “ $it$i_squaret$i_cuben”;
}
 foreach $i (1..10) {
$i_square=$i*$i; $i_cube=$i**3;
print “ $it$i_squaret$i_cuben”; }
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

More Related Content

PPTX
Perl names values and variables
PPTX
Unit 1-perl names values and variables
PPTX
Strings,patterns and regular expressions in perl
PPTX
Delegates and events in C#
PPTX
Encapsulation C++
PPTX
Public Key Cryptosystem
PPTX
COMPILER DESIGN
Perl names values and variables
Unit 1-perl names values and variables
Strings,patterns and regular expressions in perl
Delegates and events in C#
Encapsulation C++
Public Key Cryptosystem
COMPILER DESIGN

What's hot (20)

PPT
Decision making and branching
PDF
JavaScript - Chapter 10 - Strings and Arrays
PPT
Java Programming for Designers
PPTX
Arrays in Java
PPTX
Lexical analyzer generator lex
PDF
Java programming lab manual
PPSX
Data types, Variables, Expressions & Arithmetic Operators in java
PPTX
[OOP - Lec 08] Encapsulation (Information Hiding)
PPTX
Compiler design syntax analysis
PPTX
Java script
PPTX
Macro Processor
PPTX
Control structures in java
PPTX
Structure of the compiler
PDF
Python programming : Standard Input and Output
PPT
Js ppt
PPT
Cloud and dynamic infrastructure
PPT
Repetition Structure
PPTX
Interactive debugging system
PPTX
Specification-of-tokens
PPT
Decision making and looping
Decision making and branching
JavaScript - Chapter 10 - Strings and Arrays
Java Programming for Designers
Arrays in Java
Lexical analyzer generator lex
Java programming lab manual
Data types, Variables, Expressions & Arithmetic Operators in java
[OOP - Lec 08] Encapsulation (Information Hiding)
Compiler design syntax analysis
Java script
Macro Processor
Control structures in java
Structure of the compiler
Python programming : Standard Input and Output
Js ppt
Cloud and dynamic infrastructure
Repetition Structure
Interactive debugging system
Specification-of-tokens
Decision making and looping
Ad

Viewers also liked (18)

PPTX
Unit 1-strings,patterns and regular expressions
PPTX
Unit 1-introduction to perl
PPTX
Unit 1-subroutines in perl
PPTX
Unit 1-uses for scripting languages,web scripting
PPTX
Unit 1-array,lists and hashes
PPTX
Unit 1-introduction to scripts
DOCX
Soil mechanics practicals manual.(Jyoti Anischit)
PPTX
Fashion spread outfit 2
PPTX
днз №22 фестиваль. джерело батьківських знань.
PPTX
Thriller pitch
PPT
3/13 announcement
PDF
Shortpdf 131208151245-phpapp02
PDF
Aplicación de las NIIF y NIC para PYMES
DOCX
Planning booklet
PPTX
Seguridad del paciente karin
PPTX
гунько н.в. майстер клас. фестиваль. джерело батьківських знань
PPTX
Fashion spread outfit 3
PPTX
Virvoitusjuomavero 7 pointtia
Unit 1-strings,patterns and regular expressions
Unit 1-introduction to perl
Unit 1-subroutines in perl
Unit 1-uses for scripting languages,web scripting
Unit 1-array,lists and hashes
Unit 1-introduction to scripts
Soil mechanics practicals manual.(Jyoti Anischit)
Fashion spread outfit 2
днз №22 фестиваль. джерело батьківських знань.
Thriller pitch
3/13 announcement
Shortpdf 131208151245-phpapp02
Aplicación de las NIIF y NIC para PYMES
Planning booklet
Seguridad del paciente karin
гунько н.в. майстер клас. фестиваль. джерело батьківських знань
Fashion spread outfit 3
Virvoitusjuomavero 7 pointtia
Ad

Similar to Unit 1-scalar expressions and control structures (20)

PPTX
Scalar expressions and control structures in perl
PPTX
Perl slid
PDF
Perl_Part2
PPTX
File handle in PROGRAMMable extensible interpreted .pptx
PPTX
PPT
Perl 101 - The Basics of Perl Programming
PPTX
Perl courseparti
PPT
Introduction to Perl
PPT
Learn perl in amc square learning
PPT
Perl Basics for Pentesters Part 1
PDF
Cs3430 lecture 15
DOCX
PERL for QA - Important Commands and applications
PDF
newperl5
PDF
newperl5
PDF
Lecture19-20
PDF
Lecture19-20
PDF
Lecture 22
PDF
WEB PROGRAMMING UNIT VI BY BHAVSINGH MALOTH
Scalar expressions and control structures in perl
Perl slid
Perl_Part2
File handle in PROGRAMMable extensible interpreted .pptx
Perl 101 - The Basics of Perl Programming
Perl courseparti
Introduction to Perl
Learn perl in amc square learning
Perl Basics for Pentesters Part 1
Cs3430 lecture 15
PERL for QA - Important Commands and applications
newperl5
newperl5
Lecture19-20
Lecture19-20
Lecture 22
WEB PROGRAMMING UNIT VI BY BHAVSINGH MALOTH

More from sana mateen (20)

PPTX
PPTX
PHP Variables and scopes
PPTX
Php intro
PPTX
Php and web forms
PPTX
PPTX
Files in php
PPTX
File upload php
PPTX
Regex posix
PPTX
Encryption in php
PPTX
Authentication methods
PPTX
Xml schema
PPTX
Xml dtd
PPTX
Xml dom
PPTX
PPTX
Intro xml
PPTX
Dom parser
PPTX
Uses for scripting languages,web scripting in perl
PPTX
Subroutines in perl
PPTX
Array,lists and hashes in perl
PPTX
Data structure in perl
PHP Variables and scopes
Php intro
Php and web forms
Files in php
File upload php
Regex posix
Encryption in php
Authentication methods
Xml schema
Xml dtd
Xml dom
Intro xml
Dom parser
Uses for scripting languages,web scripting in perl
Subroutines in perl
Array,lists and hashes in perl
Data structure in perl

Recently uploaded (20)

PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PDF
composite construction of structures.pdf
PPTX
bas. eng. economics group 4 presentation 1.pptx
PDF
PPT on Performance Review to get promotions
PPTX
Geodesy 1.pptx...............................................
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPTX
additive manufacturing of ss316l using mig welding
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPT
Mechanical Engineering MATERIALS Selection
PDF
Well-logging-methods_new................
PPTX
web development for engineering and engineering
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
Internet of Things (IOT) - A guide to understanding
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PPTX
Sustainable Sites - Green Building Construction
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
composite construction of structures.pdf
bas. eng. economics group 4 presentation 1.pptx
PPT on Performance Review to get promotions
Geodesy 1.pptx...............................................
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
additive manufacturing of ss316l using mig welding
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Mechanical Engineering MATERIALS Selection
Well-logging-methods_new................
web development for engineering and engineering
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
Operating System & Kernel Study Guide-1 - converted.pdf
CH1 Production IntroductoryConcepts.pptx
Internet of Things (IOT) - A guide to understanding
R24 SURVEYING LAB MANUAL for civil enggi
Sustainable Sites - Green Building Construction
Embodied AI: Ushering in the Next Era of Intelligent Systems

Unit 1-scalar expressions and control structures

  • 1. 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 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 increment Binary arithmetic operation: $a+=3; $a=a+3;
  • 2.  2)String Operators In 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 minus If 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”
  • 3.  3) Comparison operator Values 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 “OKn” 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_exp eg: $a=($a<0)?0:$a;
  • 4. 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)
  • 5. CONDITIONAL EXPRESSIONS  Conditional Expressions should be in brackets.  If-then-else statement: if($total> 0){ print “$totaln” } else { print “wrong total ! n” }  If-elsif statement if($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 “OKn” if $volts>=1.5;
  • 6. 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.
  • 7.  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 “ $it$i_squaret$i_cuben”; }  foreach $i (1..10) { $i_square=$i*$i; $i_cube=$i**3; print “ $it$i_squaret$i_cuben”; }
  • 8. 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