CONTROL STRUCTURES
Control Structures
A program is a collection of statements. After the program
executes one statement, it "moves" to the next statement and
executes that one.
The execution flow is a simple sequence.
Example:
$number1 = ‘ATCG’;
$number2 = ‘GGG’;
$sum = $number1.$number2;
Print $num;
Output: ?
Control structures:
if, else, elsif, switch, while, until, do…while, for and for…each.
1. if
Flow of Control – ‘if’
Flow of Control – ‘if-else’
The condition in an if statement can be any expression.
The condition is usually one that is built from relational or
logical operators.
For example,
$x > $y
is a condition that is true if the value of $x is greater than the
value of $y
Relational Operators: operators that compare 2 expressions
Numeric relational operators
Comparing Strings
1. Sample Program using ‘if’ and ‘if else’
$sequence="ATGC";
if(defined $seq)
{
print "defined";
}
else
{
print "not defined";
}
Output: ???
2. Sample Program using ‘if’ and ‘if else’
print "Enter 2 numbers, one per linen";
$a = <STDIN>;
$b = <STDIN>;
if
( $a < $b ) {
print "$a then $bn";
}
else
{
print "$b then $an";
}
elsif
print "Enter 2 numbers, one per linen";
$a = <STDIN>;
$b = <STDIN>;
if
( $a < $b ) {
print "$a is less than $bn";
}
elsif
( $b < $a ) {
print "$b is less than $an";
}
else
{
print "The numbers are equal.n";
}
1. Sample program using ‘elsif’
TYPES OF CONTROL STRUCTURES SEEN IN PERL.ppt

TYPES OF CONTROL STRUCTURES SEEN IN PERL.ppt

  • 1.
  • 2.
    Control Structures A programis a collection of statements. After the program executes one statement, it "moves" to the next statement and executes that one. The execution flow is a simple sequence. Example: $number1 = ‘ATCG’; $number2 = ‘GGG’; $sum = $number1.$number2; Print $num; Output: ?
  • 3.
    Control structures: if, else,elsif, switch, while, until, do…while, for and for…each. 1. if
  • 4.
    Flow of Control– ‘if’
  • 5.
    Flow of Control– ‘if-else’
  • 6.
    The condition inan if statement can be any expression. The condition is usually one that is built from relational or logical operators. For example, $x > $y is a condition that is true if the value of $x is greater than the value of $y Relational Operators: operators that compare 2 expressions
  • 7.
  • 8.
  • 10.
    1. Sample Programusing ‘if’ and ‘if else’ $sequence="ATGC"; if(defined $seq) { print "defined"; } else { print "not defined"; } Output: ???
  • 11.
    2. Sample Programusing ‘if’ and ‘if else’ print "Enter 2 numbers, one per linen"; $a = <STDIN>; $b = <STDIN>; if ( $a < $b ) { print "$a then $bn"; } else { print "$b then $an"; }
  • 12.
  • 14.
    print "Enter 2numbers, one per linen"; $a = <STDIN>; $b = <STDIN>; if ( $a < $b ) { print "$a is less than $bn"; } elsif ( $b < $a ) { print "$b is less than $an"; } else { print "The numbers are equal.n"; } 1. Sample program using ‘elsif’