PERL/C2/More-Conditional-statements/English-timed

From Script | Spoken-Tutorial
Revision as of 10:51, 10 July 2014 by Gaurav (Talk | contribs)

Jump to: navigation, search
Time Narration
00:00 Welcome to the spoken tutorial on if-elsif-else and switch conditional statements in Perl.
00:07 In this tutorial, we will learn about
00:10 The if-elsif-else statement and switch statement in Perl
00:15 I am using Ubuntu Linux12.04 operating system and Perl 5.14.2
00:22 I will also be using the gedit Text Editor.
00:25 You can use any text editor of your choice.
00:29 You should have basic knowledge of Variables and Comments in Perl.
00:34 and knowledge of for, foreach, while and do-while loops and
00:38 if and if-else statements will be an added advantage.
00:43 Please go through the relevant spoken tutorials on the spoken tutorial website.
00:48 If-elsif-else statement in Perl can be used
00:52 to test multiple conditions and
00:54 When all conditions fail then it executes the default else block
00:59 The syntax of if-elsif-else statement is as follows
01:04 if space open bracket condition close bracket space Open curly bracket press Enter
01:13 piece of code to be executed when the condition is true semicolon
01:18 Press Enter
01:20 Close curly bracket space elsif space open bracket condition close bracket space open curly bracket
01:30 Press Enter
01:31 another piece of code semicolon
01:33 to be executed when elsif condition is true
01:37 Press Enter
01:39 close curly bracket space else space open curly bracket
01:44 Enter
01:45 code semicolon to be executed when both the above conditions are false semicolon
01:51 Press Enter
01:52 close curly bracket
01:55 First, the if condition is checked and executed if the condition is true.
02:01 If not, then the else if condition is checked and executed if it is true.
02:06 Otherwise, the code within else block is executed.
02:11 Now let us look at an example of if-elsif-else statement.
02:16 Open the Terminal and type
02:19 gedit conditionalBlocks dot pl space ampersand
02:26 and press Enter
02:28 This will open the conditionalBlocks.pl file in gedit.
02:33 Type the following piece of code as displayed on the screen.
02:38 We have assigned the value 'Perl' to variable language.
02:44 Note that, eq is string comparison operator.
02:49 And then we have the various conditons that we want to check.
02:55 Now, press ctrl+s to save the file.


02:58 Then switch to terminal and execute file directly.
03:02 Type, perl conditionalBlocks dot pl
03:09 Note: I am skipping the compilation step. It is not a mandatory step for executing Perl scripts.
03:16 If there is any compilation error,
03:18 execution will throw an error and will stop execution of script
03:23 Now press Enter.
03:25 The output shown on the terminal is
03:27 Hi, I am Perl
03:29 Now, let us look at our next case.
03:31 Switch to gedit.
03:33 Assign 'Java' to variable language as shown.
03:37 Press ctrl+s to save the file.
03:40 Switch to the terminal and execute the file.
03:43 Type perl conditionalBlocks dot pl
03:50 and press Enter.
03:53 The output shown on terminal is Hi, I am Java
03:59 Again let us switch back to gedit.
04:03 Now, let us assign 'English' to the language variable.
04:07 Press ctrl+s to save the file.
04:09 Switch to the terminal and execute the file.
04:13 Type perl conditionalBlocks dot pl
04:18 and press Enter.
04:19 The output shown on terminal is
04:22 I am not a computer language
04:27 The 3 cases imply that;
04:29 Only one if block that satisfies the condition will be executed.
04:35 otherwise the default else block will get execute.
04:39 We can have multiple elsif conditions as per our requirement, like this.
04:46 Here is an assignment for you -
04:48 Write an if-elsif-else statement to print
04:51 “I am a Science graduate” if stream is science
04:55 “I am a Commerce graduate” if stream is commerce


04:59 “I am an Arts graduate” if stream is neither science or commerce
05:06 Now let us learn about the switch statement.
05:10 Till Perl 5.8, there was no switch statement in Perl.
05:14 After that, Switch module was introduced,
05:18 which provided the functionality of switch statement.
05:22 Note: Modules in Perl will be covered in subsequent tutorials.
05:27 The syntax of switch is as follows:
05:30 use Switch semicolon
05:32 Press Enter
05:34 switch space open bracket dollar value close bracket space open curly bracket
05:42 Press Enter
05:44 case space 1 space open curly bracket executes when dollar value equal to 1 close curly bracket.
05:53 Press Enter
05:55 case space single quote a single quote space open curly bracket executes when dollar value equal to single quote a single quote close curly bracket
06:09 Press Enter
06:10 else space open curly bracket executes when dollar value does not match any of the cases
06:18 Close curly bracket
06:19 Press Enter
06:20 close curly bracket
06:22 Let us understand switch using a sample program.
06:26 Open the Terminal and type
06:29 gedit sampleSwitch dot pl space ampersand
06:36 and press Enter
06:38 Now, type the sample program as shown on the screen.
06:43 Let us understand how the switch statement works.
06:46 The use Switch statement includes the Switch module inside the Perl code.
06:54 Note: We will learn about use keyword in detail in subsequent tutorials.
07:00 Now we'll test the different cases.
07:03 We have assigned 'Perl' to the variable $var
07:08 The value in variable $var is checked in the switch statement.
07:14 In the first case, it matches with the case 'Perl'.
07:19 So the code written against this case will be executed.
07:24 Press ctrl+s to save the file.
07:27 Now, switch to terminal and execute the script
07:31 perl sampleSwitch.pl
07:36 press Enter
07:38 The following output is shown on the terminal
07:41 I am Perl
07:43 Switch back to sampleSwitch.pl in gedit.
07:48 Now, let us assign 'Linux' to the variable $var
07:52 Press Ctrl S to save the file.
07:57 Again, the value of variable $var will be checked in switch.
08:03 It matches with the case 'Linux'
08:05 So code written against this case will get executed.
08:10 Now, switch to terminal and execute the script
08:15 perl sampleSwitch.pl
08:19 Press Enter
08:21 The following output is shown on the terminal
08:24 I am Linux
08:26 Switch to sampleSwitch.pl in gedit.
08:30 Similarly, if variable $var has value 'Java' , then second case will be checked.
08:38 Now, let us assign 'English' to the variable $var
08:42 Again, the value of variable $var will be checked in switch.
08:47 It does not match any of the case statements.
08:50 So the else statement will be executed.
08:54 Now, switch to terminal and execute the script
09:00 perl sampleSwitch.pl
09:07 and Press Enter
09:09 The following output is shown on the terminal -
09:12 I am not a computer language
09:17 The 3 cases imply that:
09:20 The value of the expression decides the case to be executed
09:25 Only the valid case will be executed and
09:28 When there is no valid case, then the default else case will be executed
09:35 It is not mandatory to write the else case.
09:39 In such a scenario,
09:41 if none of the cases match
09:44 then there will be no output from the switch statement.
09:48 Here is another assignment for you -
09:50 Re-write the previous assignment
09:53 given earlier in this tutorial using switch statement.
09:57 Let us summarize.
09:59 In this tutorial, we have learnt -
10:01 if-elsif-else statement and
10:04 switch statement in Perl
10:05 using sample programs.
10:08 Watch the video available at the following link


10:12 It summaries the Spoken Tutorial project
10:15 If you do not have good bandwidth, you can download and watch it
10:20 The Spoken Tutorial Project Team Conducts workshops using spoken tutorials
10:25 Gives certificates to those who pass an online test
10:30 For more details, please write to contact at spoken hyphen tutorial dot org
10:36 Spoken Tutorial Project is a part of the Talk to a Teacher project
10:40 It is supported by the National Mission on Education through ICT, MHRD, Government of India.
10:47 More information on this Mission is available at spoken hyphen tutorial dot org slash NMEICT hyphen Intro
10:58 Hope you enjoyed this Perl tutorial.
11:00 This is Amol signing off.
11:03 Thanks for joining.

Contributors and Content Editors

Gaurav, PoojaMoolya, Sandhya.np14