Trending September 2023 # How Switch Statement Works In Perl With Examples # Suggested October 2023 # Top 15 Popular | Chivangcangda.com

Trending September 2023 # How Switch Statement Works In Perl With Examples # Suggested October 2023 # Top 15 Popular

You are reading the article How Switch Statement Works In Perl With Examples updated in September 2023 on the website Chivangcangda.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 How Switch Statement Works In Perl With Examples

Introduction to Perl Switch statement

Web development, programming languages, Software testing & others

Syntax:

given(expression) { when ( first value) { statement to be executed; } when (second value) { statement to be executed; } .... ... when (nth value) { statement to be executed; } default { statement to be executed if all the cases are not matched.; } } How does Switch Statement works in Perl?

Given and when keywords are used to implement switch in Perl. given keyword is used in place of switch that handles the expression or value that needs to be evaluated. when is used in place of case that handles various conditions and based on the user input, it executes the appropriate case. That is first it checks case 1 i.e. first condition, if it matched with user input, it will execute that loop, otherwise it will transfer flow control to another case i.e. another condition. If the second condition will be matched with the user input, it will execute that case else transfer to the third case. Likewise, it will execute case until the match founds. Once the match founds with user input, it will terminate the program. If none of the mentioned conditions matched with the user input, it will execute the default block. The default block is used to execute the statements if the user input does not match with conditions.

Examples to Implement Switch Statement in Perl

Here are some examples mentioned below:

Example #1

To print the Months in Perl

Code:

use feature qw(switch say); print "Enter the Number for the Month n"; given ($month) { when('1') { say "January"; } when('2') { say "February"; } when('3') { say "March"; } when('4') { say "April"; } when('5') { say "May"; } when('6') { say "June"; } when('7') { say "July"; } when('8') { say "August"; } when('9') { say "September"; } when('10') { say "October"; } when('11') { say "November"; } when('12') { say "December"; } default { say "Please Enter valid Month Number"; } }

When user will enter 10, it will show the result as shown in the below figure

Explanation: Here we have written a program to print the specific month based on the user input. use feature qw(switch say); library is used to implement switch in Perl using given and when statement. The first program will prompt the message “Enter the Number for the Month” for the user. A variable month is used to store the value enter by the user. Then given keyword will switch the value of the month and transfer flow control to the switch conditions. Based on the user input, the appropriate condition will execute.

Example #2

To print the week days in Perl

Code:

use feature qw(switch say); print "Enter the Number for the Week n"; given ($week) { when('1') { say "Monday"; } when('2') { say "Tuesday"; } when('3') { say "Wednesday"; } when('4') { say "Thursday"; } when('5') { say "Friday"; } when('6') { say "Saturday"; } when('7') { say "Sunday"; } default { say "Please Enter valid Week Number"; } }

When user enters the values which is not mentioned in the case, it will execute the default statement and print the result as shown in the below figure.

Explanation: Here we have written a program to print the weekdays based on the user input. The example is the same as the first example. First program prompt message “Enter the Number for the Week”. week variable is used to store the user input. The execution is the same as the first example

Conclusion

In this article we have seen how when and given statements are used in Perl instead of Switch and case statement along with the syntax and programs. I hope you will find this article helpful.

Recommended Articles

We hope that this EDUCBA information on “Perl Switch” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

You're reading How Switch Statement Works In Perl With Examples

Update the detailed information about How Switch Statement Works In Perl With Examples on the Chivangcangda.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!