2
Most read
3
Most read
4
Most read
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

PPTX
Perl names values and variables
PPTX
Subroutines in perl
PPTX
Learning in AI
PPTX
Process synchronization in Operating Systems
PPTX
Advanced topics in artificial neural networks
PPTX
OOP Introduction with java programming language
PPT
multiprocessors and multicomputers
PPT
Shell programming
Perl names values and variables
Subroutines in perl
Learning in AI
Process synchronization in Operating Systems
Advanced topics in artificial neural networks
OOP Introduction with java programming language
multiprocessors and multicomputers
Shell programming

What's hot (20)

PPT
Introduction to Compiler design
PPTX
MACHINE LEARNING - GENETIC ALGORITHM
PPTX
Phases of Compiler
PPTX
Substitution techniques
PPTX
Recurrent Neural Networks (RNNs)
PDF
Query trees
PPTX
Introduction to Parallel and Distributed Computing
PDF
I. AO* SEARCH ALGORITHM
PPT
Intermediate code generation (Compiler Design)
PPT
Amortized Analysis of Algorithms
PDF
Introduction to soft computing
PPTX
Data flow architecture
PPTX
Genetic algorithms
PDF
AES-Advanced Encryption Standard
PDF
Production System in AI
PPTX
Flowchart of GA
PPTX
Loader and Its types
PPTX
PDF
Code optimization in compiler design
PDF
Vc dimension in Machine Learning
Introduction to Compiler design
MACHINE LEARNING - GENETIC ALGORITHM
Phases of Compiler
Substitution techniques
Recurrent Neural Networks (RNNs)
Query trees
Introduction to Parallel and Distributed Computing
I. AO* SEARCH ALGORITHM
Intermediate code generation (Compiler Design)
Amortized Analysis of Algorithms
Introduction to soft computing
Data flow architecture
Genetic algorithms
AES-Advanced Encryption Standard
Production System in AI
Flowchart of GA
Loader and Its types
Code optimization in compiler design
Vc dimension in Machine Learning
Ad

Viewers also liked (7)

PPTX
Uses for scripting languages,web scripting in perl
PPTX
Strings,patterns and regular expressions in perl
PPTX
Reading init param
PPTX
Jdbc in servlets
PPTX
Http request and http response
PPTX
Using cookies and sessions
PPTX
Jsp elements
Uses for scripting languages,web scripting in perl
Strings,patterns and regular expressions in perl
Reading init param
Jdbc in servlets
Http request and http response
Using cookies and sessions
Jsp elements
Ad

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

PPTX
Unit 1-scalar expressions and control structures
PDF
[C++][a] tutorial 2
PDF
Perl_Part2
PPT
Php Chapter 1 Training
PPTX
Control Structures in C
PPTX
What is c
PPT
Introduction to Perl
DOC
Java Script Language Tutorial
DOCX
Programming Fundamentals lecture 7
PPTX
lesson 2.pptx
PPTX
PHP Basics
PPTX
php programming.pptx
ODP
Advanced Perl Techniques
PPTX
Selection statements on programming with C
PPTX
What is c
PPTX
UNIT – 3.pptx for first year engineering
DOC
C fundamental
PPTX
Operators and expressions in C++
PPTX
C Programming Unit-2
PPT
FP 201 Unit 2 - Part 3
Unit 1-scalar expressions and control structures
[C++][a] tutorial 2
Perl_Part2
Php Chapter 1 Training
Control Structures in C
What is c
Introduction to Perl
Java Script Language Tutorial
Programming Fundamentals lecture 7
lesson 2.pptx
PHP Basics
php programming.pptx
Advanced Perl Techniques
Selection statements on programming with C
What is c
UNIT – 3.pptx for first year engineering
C fundamental
Operators and expressions in C++
C Programming Unit-2
FP 201 Unit 2 - Part 3

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
Unit 1-subroutines in perl
PPTX
Unit 1-uses for scripting languages,web scripting
PPTX
Unit 1-strings,patterns and regular expressions
PPTX
Unit 1-perl names values and variables
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
Unit 1-subroutines in perl
Unit 1-uses for scripting languages,web scripting
Unit 1-strings,patterns and regular expressions
Unit 1-perl names values and variables

Recently uploaded (20)

PPTX
Agentic Artificial Intelligence (Agentic AI).pptx
PDF
Beginners-Guide-to-Artificial-Intelligence.pdf
PPTX
DATA STRCUTURE LABORATORY -BCSL305(PRG1)
PDF
Cryptography and Network Security-Module-I.pdf
PPTX
CS6006 - CLOUD COMPUTING - Module - 1.pptx
PDF
IAE-V2500 Engine Airbus Family A319/320
PPTX
ARCHITECTURE AND PROGRAMMING OF EMBEDDED SYSTEMS
PPTX
chapter 1.pptx dotnet technology introduction
PDF
LS-6-Digital-Literacy (1) K12 CURRICULUM .pdf
PPT
Comprehensive Java Training Deck - Advanced topics
PPTX
BBOC407 BIOLOGY FOR ENGINEERS (CS) - MODULE 1 PART 1.pptx
PDF
ECT443_instrumentation_Engg_mod-1.pdf indroduction to instrumentation
PPTX
AI-Reporting for Emerging Technologies(BS Computer Engineering)
PDF
Using Technology to Foster Innovative Teaching Practices (www.kiu.ac.ug)
PPTX
Solar energy pdf of gitam songa hemant k
PPT
Programmable Logic Controller PLC and Industrial Automation
PDF
electrical machines course file-anna university
PPTX
Module1.pptxrjkeieuekwkwoowkemehehehrjrjrj
DOCX
An investigation of the use of recycled crumb rubber as a partial replacement...
PDF
Performance, energy consumption and costs: a comparative analysis of automati...
Agentic Artificial Intelligence (Agentic AI).pptx
Beginners-Guide-to-Artificial-Intelligence.pdf
DATA STRCUTURE LABORATORY -BCSL305(PRG1)
Cryptography and Network Security-Module-I.pdf
CS6006 - CLOUD COMPUTING - Module - 1.pptx
IAE-V2500 Engine Airbus Family A319/320
ARCHITECTURE AND PROGRAMMING OF EMBEDDED SYSTEMS
chapter 1.pptx dotnet technology introduction
LS-6-Digital-Literacy (1) K12 CURRICULUM .pdf
Comprehensive Java Training Deck - Advanced topics
BBOC407 BIOLOGY FOR ENGINEERS (CS) - MODULE 1 PART 1.pptx
ECT443_instrumentation_Engg_mod-1.pdf indroduction to instrumentation
AI-Reporting for Emerging Technologies(BS Computer Engineering)
Using Technology to Foster Innovative Teaching Practices (www.kiu.ac.ug)
Solar energy pdf of gitam songa hemant k
Programmable Logic Controller PLC and Industrial Automation
electrical machines course file-anna university
Module1.pptxrjkeieuekwkwoowkemehehehrjrjrj
An investigation of the use of recycled crumb rubber as a partial replacement...
Performance, energy consumption and costs: a comparative analysis of automati...

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