Conditional Statement

                                                                                                                         

 

Conditional Statement

Perl programming language provides following types of conditional statements.

  • if statement
  • if…else statement
  • if…elsif…else statement
  • unless statement
  • unless ..else statement
  • unless…elsif..else statement
  • switch statement

if statement:

PERL’ s if statement is similar to those of other high-level languages:

if (condition)

    { do if true }

else

    { do if false }

  • Example : A simple if statement is

if ( $a > 5 )

    {print ‘$a is greater than 5.’;}

else

    {print ‘$a is greater than 5.’;}

 

if else statement:

  • A Perl if statement can be followed by an optional else statement, which executes when the boolean expression is false.

if (condition1)

        { do if condition1 true }

else if (condition2)

       { do if condition2 is true }

else

        { do if both upper condition are false }

 

Nested if-else Statement:

  • The if statement can be nested within other if statements, either inside the blocks or as part of the else:

if (cond1)

 {   if (cond2)

               {statements}

  }

else

 {   if (cond3)

             { statements}

      else

             {statements}

   }

 

Unless statement:

  • A Perl unless statement consists of a boolean expression followed by one or more statements.
  • Syntax:

unless(boolean_expression) {

  # statement(s) will execute if the given condition is false

}

  • Example:

#!/usr/bin/perl –w

$a = 20;

unless( $a < 20 )  {

     printf  “a is not less than 20\n”;    #if condition is false then print the following

           }

 

Unless elsif statement:

  • An unless statement can be followed by an optional elsif…else statement, which is very useful to test various conditions using single unless…elsif statement.
  • Syntax:

unless(boolean_expression 1) {

     # Executes when the boolean expression 1 is false

}

elsif( boolean_expression 2) {

      # Executes when the boolean expression 2 is true

}

else  {

      # Executes when the none of the above condition is met.

                }

 

Switch Statement:

  • A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each switch case.

The following rules apply to a switch statement:

  • The switch statement takes a single scalar argument of any type, specified in parentheses.
  • The value is followed by a block which may contain one or more case statement followed by a block of Perl statement(s).
  • A case statement takes a single scalar argument and selects the appropriate type of matching between the arguments.
  • If the match is successful, the mandatory block associated with the case statement is executed.

Example:

use Switch;

switch(argument) {

case 1 { print “number 1” }

case “a” { print “string a” }

case [1..10,42] { print “number in list” }

case (\@array) { print “number in list” }

case /\w+/ { print “pattern” }

case qr/\w+/ { print “pattern” }

case (\%hash) { print “entry in hash” }

case (\&sub) { print “arg to subroutine” }

        else {  print “previous case not true” }

  }

Input from the keyboard:

  • The easiest way to get input into a Perl program is to read from the keyboard. To do this, use the <STDIN> structure (the standard input, or STDIN, is the keyboard by default).
  • You use <STDIN> to read a value and store it in a variable like this:

$a=<STDIN>;

  • Example:

print ’Enter the value a \n’ ;

$a=<STDIN>;

chomp operator:

  • Use to remove the last character .
  • Example :

$a= “This is practice time \n”;

Chomp $a; // pointer will stay in same line

String operations

  • There are relational operators for strings (actually, for non-numbers):

eq            equal to

ne            not equal to

gt             greater than

lt              less than

ge            greater than or equal to

le             less than or equal to

  • Comparisons are left-to-right, using ASCII values
  • Example :

$str1=“WelcometoPerl”;

$str2=<STDIN>;

chomp $str2;

if ($str1 eq $str2){

      print “Both string are same!\n”;}

else{

     print “both strings are not same!\n”;}

 

                                                                                                                         

 

Leave a Reply

Your email address will not be published. Required fields are marked *