SlideShare a Scribd company logo
BY
SANA MATEEN
SCALAR EXPRESSIONS AND
CONTROL STRUCTURES
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

What's hot

Unit 1-introduction to scripts
Unit 1-introduction to scriptsUnit 1-introduction to scripts
Unit 1-introduction to scripts
sana mateen
 
Wireshark Basic Presentation
Wireshark Basic PresentationWireshark Basic Presentation
Wireshark Basic Presentation
MD. SHORIFUL ISLAM
 
Code Optimization
Code OptimizationCode Optimization
Code Optimization
Akhil Kaushik
 
Java Streams
Java StreamsJava Streams
Java Streams
M Vishnuvardhan Reddy
 
Run time storage
Run time storageRun time storage
Run time storage
Rasineni Madhan Mohan Naidu
 
Unit 5
Unit  5Unit  5
Unit 5
Ravi Kumar
 
Java program structure
Java program structureJava program structure
Java program structure
shalinikarunakaran1
 
sl slides-unit-1.pptx
sl slides-unit-1.pptxsl slides-unit-1.pptx
sl slides-unit-1.pptx
SRAVANTHISALLARAM1
 
Strings,patterns and regular expressions in perl
Strings,patterns and regular expressions in perlStrings,patterns and regular expressions in perl
Strings,patterns and regular expressions in perl
sana mateen
 
Symbol table in compiler Design
Symbol table in compiler DesignSymbol table in compiler Design
Symbol table in compiler Design
Kuppusamy P
 
Computer Network notes (handwritten) UNIT 1
Computer Network notes (handwritten) UNIT 1Computer Network notes (handwritten) UNIT 1
Computer Network notes (handwritten) UNIT 1
NANDINI SHARMA
 
Spm unit 3
Spm unit 3Spm unit 3
Spm unit 3
Naga Dinesh
 
1.1 The nature of software.ppt
1.1 The nature of software.ppt1.1 The nature of software.ppt
1.1 The nature of software.ppt
JAYAPRIYAR7
 
Client side scripting and server side scripting
Client side scripting and server side scriptingClient side scripting and server side scripting
Client side scripting and server side scripting
baabtra.com - No. 1 supplier of quality freshers
 
Php and MySQL
Php and MySQLPhp and MySQL
Php and MySQL
Tiji Thomas
 
Introduction to Compiler design
Introduction to Compiler design Introduction to Compiler design
Introduction to Compiler design
Dr. C.V. Suresh Babu
 
software cost factor
software cost factorsoftware cost factor
software cost factor
Abinaya B
 
Firewall Basing
Firewall BasingFirewall Basing
Firewall Basing
Pina Parmar
 

What's hot (20)

Unit 1-introduction to scripts
Unit 1-introduction to scriptsUnit 1-introduction to scripts
Unit 1-introduction to scripts
 
Wireshark Basic Presentation
Wireshark Basic PresentationWireshark Basic Presentation
Wireshark Basic Presentation
 
Code Optimization
Code OptimizationCode Optimization
Code Optimization
 
Java Streams
Java StreamsJava Streams
Java Streams
 
Operators in PHP
Operators in PHPOperators in PHP
Operators in PHP
 
Run time storage
Run time storageRun time storage
Run time storage
 
Unit 5
Unit  5Unit  5
Unit 5
 
Java program structure
Java program structureJava program structure
Java program structure
 
sl slides-unit-1.pptx
sl slides-unit-1.pptxsl slides-unit-1.pptx
sl slides-unit-1.pptx
 
Strings,patterns and regular expressions in perl
Strings,patterns and regular expressions in perlStrings,patterns and regular expressions in perl
Strings,patterns and regular expressions in perl
 
Spm unit2
Spm unit2Spm unit2
Spm unit2
 
Symbol table in compiler Design
Symbol table in compiler DesignSymbol table in compiler Design
Symbol table in compiler Design
 
Computer Network notes (handwritten) UNIT 1
Computer Network notes (handwritten) UNIT 1Computer Network notes (handwritten) UNIT 1
Computer Network notes (handwritten) UNIT 1
 
Spm unit 3
Spm unit 3Spm unit 3
Spm unit 3
 
1.1 The nature of software.ppt
1.1 The nature of software.ppt1.1 The nature of software.ppt
1.1 The nature of software.ppt
 
Client side scripting and server side scripting
Client side scripting and server side scriptingClient side scripting and server side scripting
Client side scripting and server side scripting
 
Php and MySQL
Php and MySQLPhp and MySQL
Php and MySQL
 
Introduction to Compiler design
Introduction to Compiler design Introduction to Compiler design
Introduction to Compiler design
 
software cost factor
software cost factorsoftware cost factor
software cost factor
 
Firewall Basing
Firewall BasingFirewall Basing
Firewall Basing
 

Viewers also liked

Subroutines in perl
Subroutines in perlSubroutines in perl
Subroutines in perl
sana mateen
 
Uses for scripting languages,web scripting in perl
Uses for scripting languages,web scripting in perlUses for scripting languages,web scripting in perl
Uses for scripting languages,web scripting in perl
sana mateen
 
Reading init param
Reading init paramReading init param
Reading init param
Nuha Noor
 
Jdbc in servlets
Jdbc in servletsJdbc in servlets
Jdbc in servlets
Nuha Noor
 
Http request and http response
Http request and http responseHttp request and http response
Http request and http response
Nuha Noor
 
Using cookies and sessions
Using cookies and sessionsUsing cookies and sessions
Using cookies and sessions
Nuha Noor
 
Jsp elements
Jsp elementsJsp elements
Jsp elements
Nuha Noor
 

Viewers also liked (7)

Subroutines in perl
Subroutines in perlSubroutines in perl
Subroutines in perl
 
Uses for scripting languages,web scripting in perl
Uses for scripting languages,web scripting in perlUses for scripting languages,web scripting in perl
Uses for scripting languages,web scripting in perl
 
Reading init param
Reading init paramReading init param
Reading init param
 
Jdbc in servlets
Jdbc in servletsJdbc in servlets
Jdbc in servlets
 
Http request and http response
Http request and http responseHttp request and http response
Http request and http response
 
Using cookies and sessions
Using cookies and sessionsUsing cookies and sessions
Using cookies and sessions
 
Jsp elements
Jsp elementsJsp elements
Jsp elements
 

Similar to Scalar expressions and control structures in perl

Unit 1-scalar expressions and control structures
Unit 1-scalar expressions and control structuresUnit 1-scalar expressions and control structures
Unit 1-scalar expressions and control structures
sana mateen
 
[C++][a] tutorial 2
[C++][a] tutorial 2[C++][a] tutorial 2
[C++][a] tutorial 2yasir_cesc
 
Php Chapter 1 Training
Php Chapter 1 TrainingPhp Chapter 1 Training
Php Chapter 1 Training
Chris Chubb
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in C
sana shaikh
 
What is c
What is cWhat is c
What is c
Nitesh Saitwal
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
NBACriteria2SICET
 
Java Script Language Tutorial
Java Script Language TutorialJava Script Language Tutorial
Java Script Language Tutorialvikram singh
 
Programming Fundamentals lecture 7
Programming Fundamentals lecture 7Programming Fundamentals lecture 7
Programming Fundamentals lecture 7
REHAN IJAZ
 
lesson 2.pptx
lesson 2.pptxlesson 2.pptx
lesson 2.pptx
khaledahmed316
 
PHP Basics
PHP BasicsPHP Basics
PHP Basics
Muthuganesh S
 
php programming.pptx
php programming.pptxphp programming.pptx
php programming.pptx
rani marri
 
Advanced Perl Techniques
Advanced Perl TechniquesAdvanced Perl Techniques
Advanced Perl Techniques
Dave Cross
 
Operators and expressions in C++
Operators and expressions in C++Operators and expressions in C++
Operators and expressions in C++
Neeru Mittal
 
C Programming Unit-2
C Programming Unit-2C Programming Unit-2
C Programming Unit-2
Vikram Nandini
 
FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3rohassanie
 
Control Structures
Control StructuresControl Structures
Control StructuresGhaffar Khan
 

Similar to Scalar expressions and control structures in perl (20)

Unit 1-scalar expressions and control structures
Unit 1-scalar expressions and control structuresUnit 1-scalar expressions and control structures
Unit 1-scalar expressions and control structures
 
[C++][a] tutorial 2
[C++][a] tutorial 2[C++][a] tutorial 2
[C++][a] tutorial 2
 
Perl_Part2
Perl_Part2Perl_Part2
Perl_Part2
 
Php Chapter 1 Training
Php Chapter 1 TrainingPhp Chapter 1 Training
Php Chapter 1 Training
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in C
 
What is c
What is cWhat is c
What is c
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
 
Java Script Language Tutorial
Java Script Language TutorialJava Script Language Tutorial
Java Script Language Tutorial
 
Programming Fundamentals lecture 7
Programming Fundamentals lecture 7Programming Fundamentals lecture 7
Programming Fundamentals lecture 7
 
lesson 2.pptx
lesson 2.pptxlesson 2.pptx
lesson 2.pptx
 
PHP Basics
PHP BasicsPHP Basics
PHP Basics
 
php programming.pptx
php programming.pptxphp programming.pptx
php programming.pptx
 
Advanced Perl Techniques
Advanced Perl TechniquesAdvanced Perl Techniques
Advanced Perl Techniques
 
What is c
What is cWhat is c
What is c
 
C fundamental
C fundamentalC fundamental
C fundamental
 
Operators and expressions in C++
Operators and expressions in C++Operators and expressions in C++
Operators and expressions in C++
 
C Programming Unit-2
C Programming Unit-2C Programming Unit-2
C Programming Unit-2
 
FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3
 
Control Structures
Control StructuresControl Structures
Control Structures
 
Looping statements
Looping statementsLooping statements
Looping statements
 

More from sana mateen

Files
FilesFiles
PHP Variables and scopes
PHP Variables and scopesPHP Variables and scopes
PHP Variables and scopes
sana mateen
 
Php intro
Php introPhp intro
Php intro
sana mateen
 
Php and web forms
Php and web formsPhp and web forms
Php and web forms
sana mateen
 
Mail
MailMail
Files in php
Files in phpFiles in php
Files in php
sana mateen
 
File upload php
File upload phpFile upload php
File upload php
sana mateen
 
Regex posix
Regex posixRegex posix
Regex posix
sana mateen
 
Encryption in php
Encryption in phpEncryption in php
Encryption in php
sana mateen
 
Authentication methods
Authentication methodsAuthentication methods
Authentication methods
sana mateen
 
Xml schema
Xml schemaXml schema
Xml schema
sana mateen
 
Xml dtd
Xml dtdXml dtd
Xml dtd
sana mateen
 
Xml dom
Xml domXml dom
Xml dom
sana mateen
 
Xhtml
XhtmlXhtml
Intro xml
Intro xmlIntro xml
Intro xml
sana mateen
 
Dom parser
Dom parserDom parser
Dom parser
sana mateen
 
Unit 1-subroutines in perl
Unit 1-subroutines in perlUnit 1-subroutines in perl
Unit 1-subroutines in perl
sana mateen
 
Unit 1-strings,patterns and regular expressions
Unit 1-strings,patterns and regular expressionsUnit 1-strings,patterns and regular expressions
Unit 1-strings,patterns and regular expressions
sana mateen
 
Unit 1-perl names values and variables
Unit 1-perl names values and variablesUnit 1-perl names values and variables
Unit 1-perl names values and variables
sana mateen
 
Unit 1-introduction to perl
Unit 1-introduction to perlUnit 1-introduction to perl
Unit 1-introduction to perl
sana mateen
 

More from sana mateen (20)

Files
FilesFiles
Files
 
PHP Variables and scopes
PHP Variables and scopesPHP Variables and scopes
PHP Variables and scopes
 
Php intro
Php introPhp intro
Php intro
 
Php and web forms
Php and web formsPhp and web forms
Php and web forms
 
Mail
MailMail
Mail
 
Files in php
Files in phpFiles in php
Files in php
 
File upload php
File upload phpFile upload php
File upload php
 
Regex posix
Regex posixRegex posix
Regex posix
 
Encryption in php
Encryption in phpEncryption in php
Encryption in php
 
Authentication methods
Authentication methodsAuthentication methods
Authentication methods
 
Xml schema
Xml schemaXml schema
Xml schema
 
Xml dtd
Xml dtdXml dtd
Xml dtd
 
Xml dom
Xml domXml dom
Xml dom
 
Xhtml
XhtmlXhtml
Xhtml
 
Intro xml
Intro xmlIntro xml
Intro xml
 
Dom parser
Dom parserDom parser
Dom parser
 
Unit 1-subroutines in perl
Unit 1-subroutines in perlUnit 1-subroutines in perl
Unit 1-subroutines in perl
 
Unit 1-strings,patterns and regular expressions
Unit 1-strings,patterns and regular expressionsUnit 1-strings,patterns and regular expressions
Unit 1-strings,patterns and regular expressions
 
Unit 1-perl names values and variables
Unit 1-perl names values and variablesUnit 1-perl names values and variables
Unit 1-perl names values and variables
 
Unit 1-introduction to perl
Unit 1-introduction to perlUnit 1-introduction to perl
Unit 1-introduction to perl
 

Recently uploaded

Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
MdTanvirMahtab2
 
AP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specificAP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specific
BrazilAccount1
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
Kamal Acharya
 
Runway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptxRunway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptx
SupreethSP4
 
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdfGoverning Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
WENKENLI1
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
gerogepatton
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
TeeVichai
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Sreedhar Chowdam
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
SamSarthak3
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
Massimo Talia
 
Hierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power SystemHierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power System
Kerry Sado
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
Jayaprasanna4
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
Kamal Acharya
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
R&R Consult
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
gdsczhcet
 
ASME IX(9) 2007 Full Version .pdf
ASME IX(9)  2007 Full Version       .pdfASME IX(9)  2007 Full Version       .pdf
ASME IX(9) 2007 Full Version .pdf
AhmedHussein950959
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
seandesed
 
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang,  ICLR 2024, MLILAB, KAIST AI.pdfJ.Yang,  ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
MLILAB
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Teleport Manpower Consultant
 
Investor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptxInvestor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptx
AmarGB2
 

Recently uploaded (20)

Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
 
AP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specificAP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specific
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
 
Runway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptxRunway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptx
 
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdfGoverning Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
 
Hierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power SystemHierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power System
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
 
ASME IX(9) 2007 Full Version .pdf
ASME IX(9)  2007 Full Version       .pdfASME IX(9)  2007 Full Version       .pdf
ASME IX(9) 2007 Full Version .pdf
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
 
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang,  ICLR 2024, MLILAB, KAIST AI.pdfJ.Yang,  ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
 
Investor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptxInvestor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptx
 

Scalar expressions and control structures in perl

  • 1. BY SANA MATEEN SCALAR EXPRESSIONS AND CONTROL STRUCTURES
  • 2. 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;
  • 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”
  • 4.  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;
  • 5. 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)
  • 6. 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;
  • 7. 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.
  • 8.  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”; }
  • 9. 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