Conditional Statements

When ever you want to perform a set of operations based on a condition(s) If / If-Else / Nested ifs are used.

You can also use if-else , nested IFs and IF-ELSE-IF ladder when multiple conditions are to be performed on a single variable.

1. If

Syntax

if(conditional-expression)
{
    #code
}

Example

  $x = 30;
  $y = 30;

  if ( $x == $y) {
    print "x and y are equal";
  }

Check result here

2. If-else

Syntax

if(conditional-expression)
{
    #code
} else {
    #code
}

Example

$x = 30;
$y = 20;

if ( $x == $y) {
    print "x and y are equal";
} else {
    print "x and y are not equal";  
}

Check result here

3. If-else-if ladder

Syntax

if(conditional-expression-1)
{
    #code
} elsif(conditional-expression-2) {
    #code
} elsif(conditional-expression-3) {
    #code
}
....
else {
    #code
}

Example

  $age = 79;

    if ( $age <= 1 && $age >= 0) {
      print "Infant";
    } elsif ($age > 1 && $age <= 3) {
        print "Toddler";
    } elsif ($age > 3 && $age <= 9) {
        print "Child";
    } elsif ($age > 9 && $age <= 18) {
        print "Teen";
    } elsif ($age > 18) {
        print "Adult";
    } else {
        print "Invalid $age";
    }

Check result here

4. Nested-If

Nested-Ifs represents if block within another if block.

Syntax

if(conditional-expression-1) {    
     #code    
          if(conditional-expression-2) {  
             #code
             if(conditional-expression-3) {
                 #code
             }  
    }    
}

Example

 $age = 50;
 $resident = 'Y';
 if ($age > 18)
  {
    if($resident == 'Y'){
      print "Eligible to Vote";
    }
  }

Check result here

5. Unless

Unless is similar to If and is equivalent to if-not. Perl executes the code block if the condition evaluates to false, otherwise it skips the code block.

Syntax

statement unless(condition-expression);

#or

unless(condition-expression){
   #code 
}

Example

$age = 35;
 
unless($age < 18){
   print("Major")                    
}

Check Result here

6. Unless-else

syntax

unless(condition-expression){
  # unless code
}else{
  # else code
}

Example

$x = 30;
$y = 30;

unless ( $x == $y) {
    print "x and y are not equal";
} else {
    print "x and y are equal";  
}

Check result here

7. Given

Given is similar to Switch in other programming languages. Given is an alternative to IF-ELSE-IF ladder and to select one among many blocks of code.

Syntax

given(conditional-expression){    
when(value1){#code}
when(value2){#code}
when(value3){#code}
...
}