PERL/C2/More-Conditional-statements/English
Title Of Script: if-elsif-else, switch statement
Author: Amol Brahmankar
Keywords: Conditional Statements in Perl, if-elsif-else, switch statement in perl video tutorial.
|
|
Slide | Welcome to the spoken tutorial on if-elsif-else and switch conditional statements in Perl. |
Slide: Learning Objectives | In this tutorial, we will learn about the
|
Slide: System Requirements | I am using Ubuntu Linux12.04 operating system and Perl 5.14.2
|
Slide: Prerequisites | You should have basic knowledge of Variables and Comments in Perl
will be an added advantage.
|
Slide | If-elsif-else statement in Perl can be used
|
Slide
piece of code; } elsif (condition) { another piece of code; } else { code to be executed if both the above conditions are false; } |
The syntax of if-elsif-else statement is as follows
Press Enter piece of code semicolon to be executed when the condition is true Press Enter
Press Enter another piece of code semicolon to be executed when elsif condition is true Press Enter close curly bracket space else space open curly bracket Enter code to be executed when both the above conditions are false semicolon Press Enter close curly bracket |
Slide
Highlight if (condition)
|
First, the if condition is checked and executed if the condition is true.
|
Now let us look at an example of if-elsif-else statement. | |
Switch to the Terminal and type
|
Open the Terminal and type
|
Point to the filename conditionalBlocks.pl in the Titlebar of gedit.
$language = 'Perl';
print “Hi, I am Perl\n”; } elsif ($ language eq 'Java') { print “Hi, I am Java\n”; } else { print “I am not a computer language\n”; } |
This will open the conditionalBlocks.pl file in gedit.
|
Highlight variables declaration | We have assigned the value 'Perl' to variable language. |
Highlight eq | Note that, eq is the string comparison operator. |
Highlight the if-elsif-else code | And then we have the various conditons that we want to check here. |
Press Ctrl S | Now, press ctrl+s to save the file. |
Switch to terminal | Then switch to the terminal and execute file directly. |
Type
perl conditionalBlocks.pl Don't press Enter. |
Type,
perl conditionalBlocks dot pl |
Note:
| |
Press Enter. | Now press Enter. |
Highlight the output on the terminal
|
The output shown on the terminal is
|
Switch to gedit | Now, let us look at our next case.
Switch to gedit. |
Change $language = 'Java'; | Assign 'Java' to variable language as shown. |
Press Ctrl S | Press ctrl+s to save the file. |
Switch to terminal
|
Switch to the terminal and execute the file.
Type perl conditionalBlocks dot pl
|
Highlight the output on the terminal
|
The output shown on terminal is
|
Switch to gedit | Again let us switch back to gedit. |
Change $language = 'English'; | Now, let us assign 'English' to the language variable. |
Press Ctrl S | Again, press ctrl+s to save the file. |
Switch to terminal
|
Switch to the terminal and execute the file.
perl conditionalBlocks dot pl
|
Highlight the output on the terminal
|
The output shown on terminal is
|
Slide | The 3 cases imply that;
|
Slide | We can have multiple elsif conditions as per our requirement, like this.
<<Don't read out the remaining part just display>> if (condition) { code1; } elsif (condition) { code2; } elsif (condition) { code3; } else { default_code;} |
Slide: Assignment | Here is an assignment for you -
|
<<Pause>>
Now let us learn about the switch statement. | |
Slide | Till Perl 5.8, there was no switch statement in Perl.
|
Remain on previous slide | Note:
Modules in Perl will be covered in subsequent tutorials. |
use Switch;
case 1 { executes when $value = 1} case 'a' { executes when $value = 'a'} else { executes when $value does not match any of the cases} } |
The syntax of switch is as follows:
Press Enter switch space open bracket dollar value close bracket space open curly bracket Press Enter
Press Enter
Press Enter
close curly bracket Press Enter close curly bracket |
Let us understand switch using a sample program. | |
Switch to the Terminal and type
|
Open the Terminal and type
|
use Switch;
$var = 'Perl'
case 'Perl' {print “I am Perl\n”;} case 'Java' {print “I am Java\n”;} case 'Linux' {prin “I am Linux\n”;} else {print “I am not a computer language\n”;} } |
Now, type the sample program as shown on the screen.
|
Gedit | Let us understand how the switch statement works. |
Highlight use Switch | The use Switch statement includes the Switch module inside the Perl code. |
Note:
We will learn about use keyword in detail in subsequent tutorials. | |
Now we'll test the different cases. | |
Highlight $var = 'Perl' | We have assigned 'Perl' to the variable $var |
Highlight switch ($var) | The value in variable $var is checked in the switch statement. |
Highlight case 'Perl' | In the first case, it matches with the case 'Perl'. |
Highlight {print “I am Perl\n”;} | So the code written against this case will be executed. |
Press ctrl+s | Press ctrl+s to save the file. |
Switch to terminal
perl sampleSwitch.pl |
Now, switch to terminal and execute the script
press Enter |
Terminal
Highlight the output on terminal |
The following output is shown on the terminal
I am Perl |
Switch back to sampleSwitch.pl in gedit. | |
Change $var = 'Linux' | Now, let us assign 'Linux' to the variable $var |
Highlight switch ($var) | Again, the value of variable $var will be checked in switch. |
Highlight case 'Linux' | It matches with the case 'Linux' |
Highlight {print “I am Linux\n”;} | So code written against this case will get executed. |
Switch to terminal
perl sampleSwitch.pl |
Now, switch to terminal and execute the script
|
Terminal
Highlight the output on terminal |
The following output is shown on the terminal
I am Linux |
Switch to gedit. | Switch to sampleSwitch.pl in gedit. |
Change $var = ''Java'' >>
Highlight case 'Java'
|
Similarly, if variable $var has value 'Java', then second case will be checked. |
Change $var = 'English' | Now, let us assign 'English' to the variable $var |
Highlight switch ($var) | Again, the value of variable $var will be checked in switch. |
Point to the 3 case statements >> then point to the else statement | It does not match any of the case statements.
|
Switch to Highlight
perl sampleSwitch.pl |
Now, switch to terminal and execute the script
|
Terminal
Highlight the output on Highlight |
The following output is shown on the terminal -
I am not a computer language |
Slide | The 3 cases imply that:
|
Slide
Highlight else case |
It is not mandatory to write the else case.
|
Slide: Assignment | Here is another assignment for you -
|
Slide: Summary | Let us summarize.
In this tutorial, we have learnt -
|
About the Project | Watch the video available at the following link
download and watch it |
Spoken Tutorial Workshops | The Spoken Tutorial Project Team
test
contact at spoken hyphen tutorial dot org |
Acknowledgment | Spoken Tutorial Project is a part of the Talk to a
Teacher project
Education through ICT, MHRD, Government of India.
spoken hypen tutorial dot org slash NMEICT hyphen Intro |
Hope you enjoyed this Perl tutorial.
This is Amol Brahmankar signing off.
|