How to use a switch within a loop in Perl

Overview

In computer programming, a switch is a conditional statement that controls the program's execution flow by comparing values of variables or expressions given. Conditional statements are used independently or inside code blocks such as loops. Perl supports both for and while loops and their variants, but unlike many high-level interpreter-based languages, Perl does not support switches.

Switches in Perl

To implement switches, Perl uses a combination of the given-when syntax where given is used in place of switch and when is used in place of case. Each case has a separate block of code. If the value of the variable does not match any of the blocks, then the default block executes.

An example of a switch in Perl

Suppose we want to know if a value stored in a variable var is even or odd. Let's take a look at a code example that uses given-when in Perl.

use v5.10;
no warnings 'experimental';
$var = 31;
$rem = $var % 2;
given($rem)
{
when (0) { print "The value of Var is Even\n"; }
default { print "The value of Var is Even\n"; }
}

Code explanation

  1. use v5.10 informs the interpreter about the version.
  2. no warnings 'experimental' tells the interpreter not to show experimental warnings associated with given-when .
  3. Next, a variable $var has been initialized, and the remainder, when divided by 2, is calculated using the % operator and stored in rem.
  4. given($rem)is starting the conditional block.
  5. when(0)checks if the value of rem is 0.
  6. default case executes when no other case is executed.

given-when inside loops

Now consider a problem where we want to traverse a list of numbers and want our code to decide which values are even or odd. To solve this problem, we would require an array of numbers and a loop that could enumerate all the numbers in the array.

In Perl, this task can be done using different loops structures such as for, foreach, while, do while, and until . The relevant codes for each loop structure and given-when block are described below. We will use the previous given-when block for testing even and odd numbers for simplicity.

for loop

for loop in Perl is similar to java or python The loop variable $i is initialized, checked, and incremented within one expression before entering the loop body. In this example, the for loop is repeated until it reaches the end of the array.

use v5.10;
no warnings 'experimental';
@array = (11..20);
$length = @array;
for ($i = 1; $i < $length; $i++)
{
$var = $array[$i];
$rem = $var % 2;
given($rem)
{
when (0) { print "The value of Var is Even\n"; }
default { print "The value of Var is Odd\n"; }
}
}

Code explanation

The given-whencode block used in the above example is the same as that of the first example. However, the explanation of a few new instructions is as follows.

  • Line 5, $length=@array is calculating the length of the array.
  • The expression @array = (11..20) is populating an array of consecutive numbers from 11-20 and stores it in @array.
  • $var = $array[$i] assigns the value of the array at the index i to $var

Note that similar isntructions have been used in rest of the discussion.

foreach loop

The foreach loop in Perl uses $var to hold the value of the array one at a time. This type of loop is used for iterating the list elements instead of iterating over the entire list range.

use v5.10;
no warnings 'experimental';
@array = (11..20);
$length = @array;
$i=0;
foreach $var (@array)
{
$rem = $var % 2;
given($rem)
{
when (0) { print "The value of Var is Even\n"; }
default { print "The value of Var is Odd\n"; }
}
$i++;
}

Code explanation

In the above code, foreach $var (@array) is a construct used by foreach loop which copies each element of the array into $var .

while loop

A while loop in Perl is entry controlled loop where the condition is checked before executing the loop, it takes an expression in parathesis, and if that is true, the body of the loop executes. It is used when we are unsure how often the loop will execute, but we know the termination condition.

use v5.10;
no warnings 'experimental';
@array = (11..20);
$length = @array;
$i=0;
while ($i < $length)
{
$var = $array[$i];
$rem = $var % 2;
given($rem)
{
when (0) { print "The value of Var is Even\n"; }
default { print "The value of Var is Odd\n"; }
}
$i++;
}

do while loop

Similar to the while loop, in do while loop, the body is always executed at least once, and the termination condition is evaluated later. It is also exit controlled loop as the condition is checked after running the loop.

use v5.10;
no warnings 'experimental';
@array = (11..20);
$length = @array;
$i=0;
do
{
$var = $array[$i];
$rem = $var % 2;
given($rem)
{
when (0) { print "The value of Var is Even\n"; }
default { print "The value of Var is Odd\n"; }
}
$i++;
} while ($i > 0);

until loop

An until loop in Perl is also an entry-controlled loop. It is the opposite of while loop and runs until the condition in parenthesis is false.

use v5.10;
no warnings 'experimental';
@array = (11..20);
$length = @array;
$i=0;
until ($i > $length)
{
$var = $array[$i];
$rem = $var % 2;
given($rem)
{
when (0) { print "The value of Var is Even\n"; }
default { print "The value of Var is Odd\n"; }
}
$i++;
}

Copyright ©2024 Educative, Inc. All rights reserved