given-when statement in Perl is a substitute for long if-statements that compare a variable to several integral values.
- The
given-when statement is a multiway branch statement. It provides an easy way to dispatch execution to different parts of code based on the value of the expression.
given is a control statement that allows a value to change control of execution.
given-when in Perl is similar to the switch-case of C/C++, Python or PHP. Just like the switch statement, it also substitutes multiple if-statements with different cases.
Syntax:
given(expression)
{
when(value1) {Statement;}
when(value2) {Statement;}
default {# Code if no other case matches}
}
given-when statements also use two other keywords along with it, namely break and continue. These keywords maintain the flow of the program and help in getting out of the program execution or skipping execution at a particular value.
break: break keyword is used to break out of a when block. In Perl, there is no need to explicitly write the break after every when block. It is already defined implicitly.
continue: continue, on the other hand, moves to the next when block if first when block is correct.

In a given-when statement, a conditional statement must not repeat in multiple when statements, this is because Perl checks for the first occurrence of that condition only and the next repeating statements will be ignored. Also, a default statement must be placed after all the when statements because the compiler checks for condition matching with every when statement in order, and if we will place default in between, then it will take a break over there and will print the default statement.
Example:
use 5.010;
print "Enter a day-code between 0-6\n";
chomp(my $day_code = <>);
given ($day_code)
{
when ('0') { print 'Sunday' ;}
when ('1') { print 'Monday' ;}
when ('2') { print 'Tuesday' ;}
when ('3') { print 'Wednesday' ;}
when ('4') { print 'Thursday' ;}
when ('5') { print 'Friday' ;}
when ('6') { print 'Saturday' ;}
default { print 'Invalid day-code';}
}
|
Input:
4
Output:
Enter a day-code between 0-6
Thursday
Nested given-when statement
Nested given-when statement refers to given-when statements inside of another given-when Statements. This can be used to maintain a hierarchy of inputs provided by the user for a specific output set.
Syntax:
given(expression)
{
when(value1) {Statement;}
when(value2) {given(expression)
{
when(value3) {Statement;}
when(value4) {Statement;}
when(value5) {Statement;}
default{# Code if no other case matches}
}
}
when(value6) {Statement;}
default {# Code if no other case matches}
}
Following is an example for Nested given-when statement:
use 5.010;
print "Enter a day-code between 0-6\n";
chomp(my $day_code = <>);
given ($day_code)
{
when ('0') { print 'Sunday' ;}
when ('1') { print "What time of day is it?\n";
chomp(my $day_time = <>);
given($day_time)
{
when('Morning') {print 'It is Monday Morning'};
when('Noon') {print 'It is Monday noon'};
when('Evening') {print 'It is Monday Evening'};
default{print'Invalid Input'};
}
}
when ('2') { print 'Tuesday' ;}
when ('3') { print 'Wednesday' ;}
when ('4') { print 'Thursday' ;}
when ('5') { print 'Friday' ;}
when ('6') { print 'Saturday' ;}
default { print 'Invalid day-code';}
}
|
Input:
1
Morning
Output:
Enter a day-code between 0-6
What time of day is it?
It is Monday Morning
Input:
3
Output:
Enter a day-code between 0-6
Wednesday
In the above-given Example, when the Input day-code is anything but 1 then the code will not go into the nested given-when block and the output will be same as in the previous example, but if we give 1 as Input then it will execute the Nested given-when block and the output will vary from the previous example.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
04 Jul, 2019
Like Article
Save Article