Difference between revisions of "PERL/C2/More-Conditional-statements/English-timed"

From Script | Spoken-Tutorial
Jump to: navigation, search
 
(6 intermediate revisions by 2 users not shown)
Line 5: Line 5:
 
|-
 
|-
 
| 00:00
 
| 00:00
|Welcome to the spoken tutorial on ''' if-elsif-else ''' and '''switch''' conditional statements in Perl.
+
|Welcome to the spoken tutorial on ''' if-elsif-else ''' and '''switch''' '''conditional statements''' in '''PERL'''.
 
   
 
   
 
|-
 
|-
 
| 00:07
 
| 00:07
| In this tutorial, we will learn about  
+
|In this tutorial, we will learn about  
  
 
|-
 
|-
 
|00:10
 
|00:10
|The ''' if-elsif-else''' statement and ''' switch''' statement in '''Perl '''
+
|the '''if-elsif-else''' statement and ''' switch''' statement in '''Perl '''.
 
   
 
   
 
|-
 
|-
 
|00:15
 
|00:15
| I am using '''Ubuntu Linux12.04''' operating system and '''Perl 5.14.2'''
+
| I am using '''Ubuntu Linux 12.04''' operating system and '''Perl 5.14.2'''.
 
   
 
   
 
|-
 
|-
Line 29: Line 29:
 
|-
 
|-
 
| 00:29
 
| 00:29
| You should have basic knowledge of '''Variables '''and''' Comments '''in''' Perl.'''
+
| You should have basic knowledge of variables and '''comments '''in''' Perl'''
 
   
 
   
 
|-
 
|-
Line 37: Line 37:
 
|-
 
|-
 
| 00:38
 
| 00:38
| ''' if ''' and '''if-else''' statements will be an added advantage.
+
| ''' if''' and '''if-else''' statements will be an added advantage.
  
 
|-
 
|-
 
|00:43
 
|00:43
|Please go through the relevant spoken tutorials on the '''spoken tutorial '''website.
+
|Please go through the relevant spoken tutorials on the '''Spoken Tutorial '''website.
 
   
 
   
 
|-
 
|-
Line 53: Line 53:
 
|-
 
|-
 
|00:54
 
|00:54
|When all conditions fail then it executes the default '''else''' block  
+
|when all conditions fail then it executes the default '''else''' block.
 
   
 
   
 
|-
 
|-
 
|00:59
 
|00:59
|The syntax of '''if-elsif-else''' statement is as follows  
+
|The syntax of '''if-elsif-else''' statement is as follows.
 
   
 
   
 
|-
 
|-
 
|01:04
 
|01:04
|''' if space''' open bracket ''' condition''' close bracket space Open curly bracket press Enter
+
|''' if''' space open bracket ''' condition1''' close bracket space open curly bracket press '''Enter'''
 
   
 
   
 
|-
 
|-
 
|01:13
 
|01:13
|piece of code  to be executed when the condition is true semicolon
+
|piece of code  to be executed when the condition is '''true''' semicolon
 
   
 
   
 
 
|-
 
|-
 
|01:18
 
|01:18
|Press '''Enter '''
+
|press '''Enter '''.
  
 
|-
 
|-
 
| 01:20
 
| 01:20
| Close curly bracket space '''elsif''' space open bracket ''' condition''' close bracket space open curly bracket  
+
| Close curly bracket space '''elsif''' space open bracket ''' condition2''' close bracket space open curly bracket  
  
 
|-
 
|-
 
|01:30
 
|01:30
|Press '''Enter '''
+
|press '''Enter ''', another piece of code semicolon  
 
+
|-
+
|01:31
+
|another piece of code semicolon  
+
 
   
 
   
 
|-
 
|-
 
|01:33
 
|01:33
|to be executed when '''elsif condition''' is true  
+
|to be executed when '''elsif condition''' is '''true'''
 
   
 
   
 
|-
 
|-
 
|01:37
 
|01:37
|Press '''Enter '''
+
|press '''Enter '''.
 
   
 
   
 
|-
 
|-
 
|01:39
 
|01:39
|close curly bracket space ''' else'''  space open curly bracket  
+
|Close curly bracket space''' else'''  space open curly bracket  
 
+
 
   
 
   
 
|-
 
|-
 
|01:44
 
|01:44
| Enter  
+
|'''Enter'''. Code to be executed when both the above conditions are '''false''' semicolon
+
|-
+
| 01:45
+
|code semicolon  to be executed when both the above conditions are false semicolon
+
  
 
|-
 
|-
 
|01:51
 
|01:51
|Press '''Enter '''
+
|press '''Enter '''.Close curly bracket.
+
|-
+
|01:52
+
| close curly bracket  
+
  
 
|-
 
|-
 
| 01:55
 
| 01:55
| First, the ''' if ''' condition is checked and executed if the condition is true.  
+
| First, the '''if''' condition is checked and executed if the '''condition1''' is '''true'''.  
 
   
 
   
 
|-
 
|-
 
|02:01
 
|02:01
|If not, then the ''' else if''' condition is checked and executed if it is true.  
+
|If not, then the ''' else if''' condition is checked and executed if it is '''true'''.  
 
   
 
   
 
|-
 
|-
Line 131: Line 117:
 
|-
 
|-
 
|02:16
 
|02:16
|Open the Terminal and type  
+
|Open the Terminal and type:
 
   
 
   
 
|-
 
|-
 
|02:19
 
|02:19
 
| '''gedit conditionalBlocks dot pl space ampersand'''
 
| '''gedit conditionalBlocks dot pl space ampersand'''
+
 
+
 
|-
 
|-
 
|02:26
 
|02:26
| and press '''Enter'''
+
| and press '''Enter'''.
 
   
 
   
 
|-
 
|-
 
|02:28
 
|02:28
|This will open the ''' conditionalBlocks.pl''' file in '''gedit. '''
+
|This will open the " conditionalBlocks.pl" file in '''gedit. '''
 
   
 
   
 
|-
 
|-
Line 152: Line 137:
 
|-
 
|-
 
| 02:38     
 
| 02:38     
| We have assigned the value ''' 'Perl' ''' to variable language.
+
| We have assigned the value 'Perl' to the variable '''language'''.
  
 
|-
 
|-
 
|02:44
 
|02:44
|Note that, '''eq''' is  '''string comparison''' operator.  
+
|Note that '''eq''' is  '''string comparison''' operator.  
 
   
 
   
 
|-
 
|-
 
|02:49
 
|02:49
| And then we have the various conditons that we want to check.
+
|And then we have various conditions that we want to check.
 
   
 
   
 
|-
 
|-
 
|02:55
 
|02:55
|Now, press ctrl+s to save the file.  
+
|Now, press '''Ctrl+s''' to '''save''' the file.  
+
 
+
 
|-
 
|-
 
|02:58
 
|02:58
Line 173: Line 157:
 
|-
 
|-
 
| 03:02
 
| 03:02
| Type, '''perl conditionalBlocks dot pl '''
+
|Type: '''perl conditionalBlocks dot pl '''
 
+
 
   
 
   
 
|-
 
|-
Line 181: Line 164:
 
   
 
   
 
|-
 
|-
| 03:16
+
|03:16
 
| If there is any compilation error,  
 
| If there is any compilation error,  
 
   
 
   
 
|-
 
|-
|   03:18
+
|03:18
| execution will throw an error and will stop execution of script
+
| execution will throw an error and will stop execution of script.
 
   
 
   
 
 
|-
 
|-
 
| 03:23
 
| 03:23
 
| Now press '''Enter.'''
 
| Now press '''Enter.'''
 
 
   
 
   
 
|-
 
|-
Line 200: Line 181:
 
|-
 
|-
 
| 03:27
 
| 03:27
| ''' Hi, I am Perl '''
+
| "Hi, I am Perl"
 
   
 
   
 
|-
 
|-
Line 212: Line 193:
 
|-
 
|-
 
|03:33  
 
|03:33  
|Assign ''' 'Java' ''' to variable language as shown.  
+
|Assign 'Java' to variable '''language''' as shown.  
 
   
 
   
 
|-
 
|-
| 03:37
+
|03:37
| Press '''ctrl+s''' to save the file.  
+
| Press '''Ctrl+s''' to '''save''' the file.  
 
   
 
   
 
|-
 
|-
 
|03:40
 
|03:40
 
|Switch to the terminal and execute the file.  
 
|Switch to the terminal and execute the file.  
 
 
   
 
   
 
|-
 
|-
 
|03:43
 
|03:43
|Type ''' perl conditionalBlocks dot pl '''
+
|Type: '''perl conditionalBlocks dot pl '''
 
+
 
   
 
   
 
|-
 
|-
Line 233: Line 212:
 
   
 
   
 
|-
 
|-
|   03:53
+
|03:53
| The output shown on terminal is ''' Hi, I am Java '''
+
|The output shown on the terminal is "Hi, I am Java".
 
   
 
   
 
|-
 
|-
Line 242: Line 221:
 
|-
 
|-
 
| 04:03
 
| 04:03
| Now, let us assign  ''' 'English' ''' to the ''' language''' variable.  
+
| Now, let us assign  'English' to the '''language''' variable.  
  
 
|-
 
|-
Line 248: Line 227:
 
| Press ''' ctrl+s ''' to save the file.  
 
| Press ''' ctrl+s ''' to save the file.  
 
   
 
   
 
 
|-
 
|-
 
|04:09
 
|04:09
 
|Switch to the terminal and execute the file.  
 
|Switch to the terminal and execute the file.  
+
 
+
 
|-
 
|-
 
|04:13
 
|04:13
|Type '''perl conditionalBlocks dot pl '''
+
|Type: '''perl conditionalBlocks dot pl '''
 
+
 
   
 
   
 
|-
 
|-
 
|04:18
 
|04:18
|and press '''Enter.'''
+
|and press '''Enter.''' The output shown on the terminal is:
+
|-
+
|04:19
+
|The output shown on terminal is  
+
  
 
|-
 
|-
 
|04:22
 
|04:22
|'''I am not a computer language '''
+
|"I am not a computer language".
 
   
 
   
 
|-
 
|-
 
| 04:27
 
| 04:27
| The 3 cases imply that;
+
|The three cases imply that  
  
 
|-
 
|-
 
| 04:29
 
| 04:29
| Only one ''' if block''' that satisfies the condition will be executed.
+
| only one '''if block''' that satisfies the '''condition''' will be executed.
 
   
 
   
 
|-
 
|-
 
|04:35
 
|04:35
| otherwise the default ''' else block''' will get execute.  
+
| Otherwise the default '''else block''' will get executed.  
  
 
|-
 
|-
Line 297: Line 269:
 
|-
 
|-
 
|04:51
 
|04:51
|''' “I am a Science graduate” ''' if '''stream''' is ''' science '''
+
| “I am a Science graduate” if '''stream''' is 'science'  
  
 
|-
 
|-
 
|04:55
 
|04:55
|'''“I am a Commerce graduate” ''' if '''stream''' is '''commerce'''  
+
|“I am a Commerce graduate” if '''stream''' is 'commerce'
+
 
+
 
|-
 
|-
 
|04:59
 
|04:59
|''' “I am an Arts graduate” ''' if ''' stream''' is neither ''' science''' or '''commerce '''
+
| “I am an Arts graduate” if ''' stream''' is neither 'science' nor 'commerce'.
+
 
   
 
   
 
|-
 
|-
 
|05:06
 
|05:06
 
|Now let us learn about the '''switch''' statement.  
 
|Now let us learn about the '''switch''' statement.  
 
 
   
 
   
 
|-
 
|-
| 05:10
+
|05:10
 
| Till Perl 5.8, there was no '''switch''' statement in '''Perl.'''  
 
| Till Perl 5.8, there was no '''switch''' statement in '''Perl.'''  
 
   
 
   
 
|-
 
|-
| 05:14
+
|05:14
| After that, '''Switch module''' was introduced,
+
| After that, '''Switch module''' was introduced  
 
   
 
   
 
|-
 
|-
Line 328: Line 297:
 
|-
 
|-
 
|  05:22
 
|  05:22
| Note: ''' Modules''' in '''Perl''' will be covered in subsequent tutorials.  
+
| Note: '''Modules''' in '''Perl''' will be covered in subsequent tutorials.  
  
 
|-
 
|-
 
|  05:27
 
|  05:27
| The syntax of ''' switch''' is as follows:  
+
| The syntax of '''switch''' is as follows:  
 
   
 
   
 
|-
 
|-
Line 340: Line 309:
 
|-
 
|-
 
|  05:32
 
|  05:32
| Press '''Enter'''  
+
| press '''Enter'''  
  
 
|-
 
|-
 
|  05:34
 
|  05:34
| '''switch ''' space open bracket '''dollar value''' close bracket space open curly bracket  
+
| '''switch''' space open bracket '''$ (dollar) value''' close bracket space open curly bracket  
  
 
|-
 
|-
 
|05:42
 
|05:42
|Press '''Enter '''
+
|press '''Enter '''
 
   
 
   
 
|-
 
|-
 
|  05:44
 
|  05:44
|  '''case ''' space 1 space open curly bracket ''' executes when dollar value equal to 1''' close curly bracket.  
+
|  '''case''' space 1 space open curly bracket '''executes when dollar value equal to 1''' close curly bracket.  
 
   
 
   
 
|-
 
|-
 
|  05:53
 
|  05:53
| Press '''Enter'''  
+
| Press '''Enter'''.
  
 
|-
 
|-
 
|05:55
 
|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  
+
|'''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
 
| 06:09
| Press ''' Enter '''
+
| Press ''' Enter '''. ''' else''' space open curly bracket ''' executes when dollar value does not match any of the cases '''
 
+
+
|-
+
| 06:10
+
|''' else''' space open curly bracket ''' executes when dollar value does not match any of the cases '''
+
 
   
 
   
 
|-
 
|-
 
|06:18
 
|06:18
|Close curly bracket  
+
|close curly bracket. Press '''Enter '''.
  
|-
 
|06:19
 
|Press '''Enter '''
 
 
 
 
|-
 
|-
 
| 06:20
 
| 06:20
| close curly bracket  
+
| Close curly bracket.
  
 
|-
 
|-
Line 390: Line 349:
 
|-
 
|-
 
|  06:26
 
|  06:26
|  Open the Terminal and type  
+
|  Open the Terminal and type:
 
   
 
   
 
|-
 
|-
Line 398: Line 357:
 
|-
 
|-
 
|06:36
 
|06:36
|and press '''Enter'''
+
|and press '''Enter'''.
 
   
 
   
 
|-
 
|-
 
|06:38
 
|06:38
  |Now, type the sample program as shown on the screen.  
+
|Now, type the sample program as shown on the screen.  
 
 
 
 
 
|-
 
|-
Line 414: Line 373:
 
|-
 
|-
 
|06:54
 
|06:54
| Note: We will learn about '''use keyword''' in detail in subsequent tutorials.  
+
| Note: We will learn about '''use keyword''', in detail, in subsequent tutorials.  
  
 
|-
 
|-
 
|07:00
 
|07:00
| Now we'll test the different cases.   
+
| Now we will test the different cases.   
  
 
|-
 
|-
 
|07:03
 
|07:03
| We have assigned ''' 'Perl' ''' to the variable ''' $var'''
+
| We have assigned 'Perl' to the variable '''$var'''.
 
   
 
   
 
|-
 
|-
 
|07:08
 
|07:08
| The value in variable ''' $var''' is checked in the switch statement.  
+
| The value in variable '''$var''' is checked in the '''switch''' statement.  
  
 
|-
 
|-
 
|07:14
 
|07:14
| In the first case, it matches with the case ''' 'Perl'.'''
+
| In the first case, it matches with the case 'Perl'.  
  
 
|-
 
|-
 
|07:19
 
|07:19
| So the code written against this '''case''' will be executed.  
+
| So, the code written against this '''case''' will be executed.  
  
 
|-
 
|-
Line 450: Line 409:
 
|-
 
|-
 
|07:36
 
|07:36
| press '''Enter'''  
+
| press '''Enter'''.
  
 
|-
 
|-
Line 458: Line 417:
 
|-
 
|-
 
|07:41
 
|07:41
|''' I am Perl '''
+
|"I am Perl"
 
+
  
 
|-
 
|-
Line 467: Line 425:
 
|-
 
|-
 
|07:48
 
|07:48
| Now, let us assign ''' 'Linux' ''' to the variable ''' $var '''
+
| Now, let us assign 'Linux' to the variable '''$var'''.
  
 
|-
 
|-
 
|07:52
 
|07:52
|Press ''' Ctrl S''' to save the file.
+
|Press '''Ctrl+S''' to '''save''' the file.
  
 
|-
 
|-
 
|07:57
 
|07:57
| Again, the value of variable ''' $var''' will be checked in switch.  
+
| Again, the value of variable ''' $var''' will be checked in '''switch'''.  
  
 
|-
 
|-
 
|08:03
 
|08:03
| It matches with the case ''' 'Linux' '''
+
| It matches with the case 'Linux'.
  
 
|-
 
|-
 
|08:05
 
|08:05
| So code written against this case will get executed.  
+
| So, code written against this '''case''' will get executed.  
  
 
|-
 
|-
 
|08:10
 
|08:10
| Now, switch to terminal and execute the script  
+
| Now, switch to terminal and execute the script.
  
 
|-
 
|-
Line 495: Line 453:
 
|-
 
|-
 
|08:19
 
|08:19
| Press '''Enter'''
+
| press '''Enter'''.
  
 
|-
 
|-
 
|08:21
 
|08:21
| The following output is shown on the terminal  
+
| The following output is shown on the terminal.
  
 
|-
 
|-
 
|08:24
 
|08:24
| ''' I am Linux'''
+
| "I am Linux"
  
 
|-
 
|-
 
|08:26
 
|08:26
| Switch to ''' sampleSwitch.pl''' in ''' gedit.'''  
+
| Switch to '''sampleSwitch.pl''' in ''' gedit.'''  
  
 
|-
 
|-
 
|08:30
 
|08:30
| Similarly, if variable ''' $var''' has value ''' 'Java' ''', then second case will be checked.  
+
| Similarly, if variable '''$var''' has value 'Java' then second '''case''' will be checked.  
  
 
|-
 
|-
 
|08:38
 
|08:38
| Now, let us assign ''' 'English' ''' to the variable ''' $var'''
+
| Now, let us assign 'English' to the variable '''$var'''.
  
 
|-
 
|-
 
|08:42
 
|08:42
| Again, the value of variable ''' $var ''' will be checked in switch.  
+
| Again, the value of variable '''$var ''' will be checked in '''switch'''.  
  
 
|-
 
|-
Line 527: Line 485:
 
|-
 
|-
 
|08:50
 
|08:50
| So the ''' else ''' statement will be executed.  
+
| So, the '''else''' statement will be executed.  
  
 
|-
 
|-
 
|08:54
 
|08:54
| Now, switch to terminal and execute the script  
+
| Now, switch to terminal and execute the script.
  
 
|-
 
|-
 
|09:00
 
|09:00
| ''' perl sampleSwitch.pl'''
+
| '''perl sampleSwitch.pl'''
  
 
|-
 
|-
 
|09:07
 
|09:07
| and Press ''' Enter'''
+
| and press '''Enter'''.
  
 
|-
 
|-
 
|09:09
 
|09:09
| The following output is shown on the terminal -
+
| The following output is shown on the terminal
  
 
|-
 
|-
 
|09:12
 
|09:12
|''' I am not a computer language '''
+
|"I am not a computer language"
 
+
  
 
|-
 
|-
 
|09:17
 
|09:17
| The 3 cases imply that:  
+
| The three cases imply that:  
  
 
|-
 
|-
 
|09:20
 
|09:20
| The value of the expression decides the ''' case ''' to be executed  
+
| The value of the expression decides the '''case''' to be executed.
  
 
|-
 
|-
 
|09:25
 
|09:25
| Only the valid ''' case''' will be executed and  
+
| Only the valid '''case''' will be executed and  
  
 
|-
 
|-
 
|09:28
 
|09:28
| When there is no valid '''case''', then the default ''' else ''' case will be executed  
+
| when there is no valid '''case''' then the default '''else''' '''case''' will be executed.
  
 
|-
 
|-
 
|09:35
 
|09:35
| It is not mandatory to write the '''else case. '''
+
| It is not mandatory to write the '''else case.'''
  
 
|-
 
|-
 
|09:39
 
|09:39
| In such a scenario,  
+
| In such scenario,  
  
 
|-
 
|-
Line 580: Line 537:
 
|-
 
|-
 
|09:44
 
|09:44
| then there will be no output from the ''' switch''' statement.  
+
| then there will be no output from the '''switch''' statement.  
  
 
|-
 
|-
 
|09:48
 
|09:48
| Here is another assignment for you -
+
| Here is another assignment for you:
  
 
|-
 
|-
Line 592: Line 549:
 
|-
 
|-
 
|09:53
 
|09:53
| given earlier in this tutorial using ''' switch''' statement.  
+
| given earlier, in this tutorial, using ''' switch''' statement.  
  
 
|-
 
|-
Line 600: Line 557:
 
|-
 
|-
 
|09:59
 
|09:59
| In this tutorial, we have learnt -
+
| In this tutorial, we have learnt
  
 
|-
 
|-
Line 608: Line 565:
 
|-
 
|-
 
|10:04
 
|10:04
| '''switch''' statement in '''Perl '''
+
| '''switch''' statement in '''Perl ''' using sample programs.  
 
+
|-
+
|10:05
+
| '''using sample programs. '''
+
  
 
|-
 
|-
 
|10:08
 
|10:08
|Watch the video available at the following link
+
|Watch the video available at the following link.
+
 
+
 
|-
 
|-
 
|10:12
 
|10:12
|It summaries the Spoken Tutorial project
+
|It summarizes the Spoken Tutorial project.
 
   
 
   
 
|-
 
|-
 
|10:15
 
|10:15
|If you do not have good bandwidth, you can download and watch it
+
|If you do not have good bandwidth, you can download and watch it.
 
   
 
   
 
|-
 
|-
 
| 10:20
 
| 10:20
| The Spoken Tutorial Project Team Conducts workshops using spoken tutorials
+
|The Spoken Tutorial project team: Conducts workshops using spoken tutorials.
 
   
 
   
 
|-
 
|-
 
|10:25
 
|10:25
|Gives certificates to those who pass an online  test
+
|Gives certificates to those who pass an online  test.
 
   
 
   
 
|-
 
|-
 
|10:30
 
|10:30
|For more details, please write to contact at spoken hyphen tutorial dot org
+
|For more details, please write to: contact at spoken hyphen tutorial dot org.
 
   
 
   
 
|-
 
|-
 
| 10:36
 
| 10:36
| Spoken Tutorial Project is a part of the Talk to a Teacher project
+
|"Spoken Tutorial" project is a part of the "Talk to a Teacher" project.
 
   
 
   
 
|-
 
|-
Line 649: Line 601:
 
|-
 
|-
 
|10:47
 
|10:47
|More information on this Mission is available at spoken hyphen tutorial dot org slash NMEICT hyphen Intro
+
|More information on this mission is available at: spoken hyphen tutorial dot org slash NMEICT hyphen Intro.
 
   
 
   
 
|-
 
|-
 
| 10:58
 
| 10:58
| Hope you enjoyed this Perl tutorial.  
+
| Hope you enjoyed this '''PERL''' tutorial.  
 
   
 
   
 
|-
 
|-
 
|11:00
 
|11:00
|This is Amol signing off.
+
|This is Amol, signing off.
 
   
 
   
 
|-
 
|-

Latest revision as of 15:37, 10 March 2017

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 Linux 12.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 condition1 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 condition2 close bracket space open curly bracket
01:30 press Enter , 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. Code to be executed when both the above conditions are false semicolon
01:51 press Enter .Close curly bracket.
01:55 First, the if condition is checked and executed if the condition1 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 the variable language.
02:44 Note that eq is string comparison operator.
02:49 And then we have various conditions 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 the 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. The output shown on the terminal is:
04:22 "I am not a computer language".
04:27 The three 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 executed.
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' nor '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 . else space open curly bracket executes when dollar value does not match any of the cases
06:18 close curly bracket. 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 will 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 three 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 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 using sample programs.
10:08 Watch the video available at the following link.
10:12 It summarizes 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