Difference between revisions of "PERL/C2/More-Conditional-statements/English-timed"
From Script | Spoken-Tutorial
PoojaMoolya (Talk | contribs) |
|||
Line 1: | Line 1: | ||
{| border=1 | {| border=1 | ||
− | + | |'''Time''' | |
− | + | |'''Narration''' | |
|- | |- | ||
− | | 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 | + | | 00:07 |
| In this tutorial, we will learn about | | In this tutorial, we will learn about | ||
|- | |- | ||
− | |00 | + | |00:10 |
|The ''' if-elsif-else''' statement and ''' switch''' statement in '''Perl ''' | |The ''' if-elsif-else''' statement and ''' switch''' statement in '''Perl ''' | ||
|- | |- | ||
− | |00 | + | |00:15 |
| I am using '''Ubuntu Linux12.04''' operating system and '''Perl 5.14.2''' | | I am using '''Ubuntu Linux12.04''' operating system and '''Perl 5.14.2''' | ||
|- | |- | ||
− | |00 | + | |00:22 |
|I will also be using the '''gedit '''Text Editor. | |I will also be using the '''gedit '''Text Editor. | ||
|- | |- | ||
− | |00 | + | |00:25 |
|You can use any text editor of your choice. | |You can use any text editor of your choice. | ||
|- | |- | ||
− | | 00 | + | | 00:29 |
| You should have basic knowledge of '''Variables '''and''' Comments '''in''' Perl.''' | | You should have basic knowledge of '''Variables '''and''' Comments '''in''' Perl.''' | ||
|- | |- | ||
− | | 00 | + | | 00:34 |
|and knowledge of ''' for, foreach, while''' and '''do-while''' loops and | |and knowledge of ''' for, foreach, while''' and '''do-while''' loops and | ||
|- | |- | ||
− | | 00 | + | | 00:38 |
| ''' if ''' and '''if-else''' statements will be an added advantage. | | ''' if ''' and '''if-else''' statements will be an added advantage. | ||
|- | |- | ||
− | |00 | + | |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. | ||
|- | |- | ||
− | |00 | + | |00:48 |
|''' If-elsif-else''' statement in '''Perl''' can be used | |''' If-elsif-else''' statement in '''Perl''' can be used | ||
|- | |- | ||
− | | 00 | + | | 00:52 |
| to test multiple conditions and | | to test multiple conditions and | ||
|- | |- | ||
− | |00 | + | |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 | + | |00:59 |
|The syntax of '''if-elsif-else''' statement is as follows | |The syntax of '''if-elsif-else''' statement is as follows | ||
|- | |- | ||
− | |01 | + | |01:04 |
|''' if space''' open bracket ''' condition''' close bracket space Open curly bracket press Enter | |''' if space''' open bracket ''' condition''' close bracket space Open curly bracket press Enter | ||
|- | |- | ||
− | |01 | + | |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 | + | |01:18 |
|Press '''Enter ''' | |Press '''Enter ''' | ||
|- | |- | ||
− | | 01 | + | | 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 ''' condition''' close bracket space open curly bracket | ||
|- | |- | ||
− | |01 | + | |01:30 |
|Press '''Enter ''' | |Press '''Enter ''' | ||
|- | |- | ||
− | |01 | + | |01:31 |
|another piece of code semicolon | |another piece of code semicolon | ||
|- | |- | ||
− | |01 | + | |01:33 |
|to be executed when '''elsif condition''' is true | |to be executed when '''elsif condition''' is true | ||
|- | |- | ||
− | |01 | + | |01:37 |
|Press '''Enter ''' | |Press '''Enter ''' | ||
|- | |- | ||
− | |01 | + | |01:39 |
|close curly bracket space ''' else''' space open curly bracket | |close curly bracket space ''' else''' space open curly bracket | ||
|- | |- | ||
− | |01 | + | |01:44 |
| Enter | | Enter | ||
|- | |- | ||
− | | 01 | + | | 01:45 |
|code semicolon to be executed when both the above conditions are false semicolon | |code semicolon to be executed when both the above conditions are false semicolon | ||
|- | |- | ||
− | |01 | + | |01:51 |
|Press '''Enter ''' | |Press '''Enter ''' | ||
|- | |- | ||
− | |01 | + | |01:52 |
| close curly bracket | | close curly bracket | ||
|- | |- | ||
− | | 01 | + | | 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 condition is true. | ||
|- | |- | ||
− | |02 | + | |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. | ||
|- | |- | ||
− | |02 | + | |02:06 |
|Otherwise, the code within '''else''' block is executed. | |Otherwise, the code within '''else''' block is executed. | ||
|- | |- | ||
− | |02 | + | |02:11 |
|Now let us look at an example of '''if-elsif-else''' statement. | |Now let us look at an example of '''if-elsif-else''' statement. | ||
|- | |- | ||
− | |02 | + | |02:16 |
|Open the Terminal and type | |Open the Terminal and type | ||
|- | |- | ||
− | |02 | + | |02:19 |
| '''gedit conditionalBlocks dot pl space ampersand''' | | '''gedit conditionalBlocks dot pl space ampersand''' | ||
|- | |- | ||
− | |02 | + | |02:26 |
| and press '''Enter''' | | and press '''Enter''' | ||
|- | |- | ||
− | |02 | + | |02:28 |
|This will open the ''' conditionalBlocks.pl''' file in '''gedit. ''' | |This will open the ''' conditionalBlocks.pl''' file in '''gedit. ''' | ||
|- | |- | ||
− | |02 | + | |02:33 |
|Type the following piece of code as displayed on the screen. | |Type the following piece of code as displayed on the screen. | ||
|- | |- | ||
− | | 02 | + | | 02:38 |
| We have assigned the value ''' 'Perl' ''' to variable language. | | We have assigned the value ''' 'Perl' ''' to variable language. | ||
|- | |- | ||
− | |02 | + | |02:44 |
|Note that, '''eq''' is '''string comparison''' operator. | |Note that, '''eq''' is '''string comparison''' operator. | ||
|- | |- | ||
− | |02 | + | |02:49 |
| And then we have the various conditons that we want to check. | | And then we have the various conditons that we want to check. | ||
|- | |- | ||
− | |02 | + | |02:55 |
|Now, press ctrl+s to save the file. | |Now, press ctrl+s to save the file. | ||
|- | |- | ||
− | |02 | + | |02:58 |
|Then switch to terminal and execute file directly. | |Then switch to terminal and execute file directly. | ||
|- | |- | ||
− | | 03 | + | | 03:02 |
| Type, '''perl conditionalBlocks dot pl ''' | | Type, '''perl conditionalBlocks dot pl ''' | ||
|- | |- | ||
− | |03 | + | |03:09 |
|Note: I am skipping the compilation step. It is not a mandatory step for executing Perl scripts. | |Note: I am skipping the compilation step. It is not a mandatory step for executing Perl scripts. | ||
|- | |- | ||
− | | 03 | + | | 03:16 |
| If there is any compilation error, | | If there is any compilation error, | ||
|- | |- | ||
− | | 03 | + | | 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 | + | | 03:23 |
| Now press '''Enter.''' | | Now press '''Enter.''' | ||
|- | |- | ||
− | |03 | + | |03:25 |
|The output shown on the terminal is | |The output shown on the terminal is | ||
|- | |- | ||
− | | 03 | + | | 03:27 |
| ''' Hi, I am Perl ''' | | ''' Hi, I am Perl ''' | ||
|- | |- | ||
− | | 03 | + | | 03:29 |
| Now, let us look at our next case. | | Now, let us look at our next case. | ||
|- | |- | ||
− | |03 | + | |03:31 |
|Switch to '''gedit. ''' | |Switch to '''gedit. ''' | ||
|- | |- | ||
− | |03 | + | |03:33 |
|Assign ''' 'Java' ''' to variable language as shown. | |Assign ''' 'Java' ''' to variable language as shown. | ||
|- | |- | ||
− | | 03 | + | | 03:37 |
| Press '''ctrl+s''' to save the file. | | Press '''ctrl+s''' to save the file. | ||
|- | |- | ||
− | |03 | + | |03:40 |
|Switch to the terminal and execute the file. | |Switch to the terminal and execute the file. | ||
|- | |- | ||
− | |03 | + | |03:43 |
|Type ''' perl conditionalBlocks dot pl ''' | |Type ''' perl conditionalBlocks dot pl ''' | ||
|- | |- | ||
− | | 03 | + | | 03:50 |
| and press '''Enter.''' | | and press '''Enter.''' | ||
|- | |- | ||
− | | 03 | + | | 03:53 |
| The output shown on terminal is ''' Hi, I am Java ''' | | The output shown on terminal is ''' Hi, I am Java ''' | ||
|- | |- | ||
− | | 03 | + | | 03:59 |
| Again let us switch back to '''gedit. ''' | | Again let us switch back to '''gedit. ''' | ||
|- | |- | ||
− | | 04 | + | | 04:03 |
| Now, let us assign ''' 'English' ''' to the ''' language''' variable. | | Now, let us assign ''' 'English' ''' to the ''' language''' variable. | ||
|- | |- | ||
− | | 04 | + | | 04:07 |
| Press ''' ctrl+s ''' to save the file. | | Press ''' ctrl+s ''' to save the file. | ||
|- | |- | ||
− | |04 | + | |04:09 |
|Switch to the terminal and execute the file. | |Switch to the terminal and execute the file. | ||
|- | |- | ||
− | |04 | + | |04:13 |
|Type '''perl conditionalBlocks dot pl ''' | |Type '''perl conditionalBlocks dot pl ''' | ||
|- | |- | ||
− | |04 | + | |04:18 |
|and press '''Enter.''' | |and press '''Enter.''' | ||
|- | |- | ||
− | |04 | + | |04:19 |
|The output shown on terminal is | |The output shown on terminal is | ||
|- | |- | ||
− | |04 | + | |04:22 |
|'''I am not a computer language ''' | |'''I am not a computer language ''' | ||
|- | |- | ||
− | | 04 | + | | 04:27 |
| The 3 cases imply that; | | The 3 cases imply that; | ||
|- | |- | ||
− | | 04 | + | | 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 | + | |04:35 |
| otherwise the default ''' else block''' will get execute. | | otherwise the default ''' else block''' will get execute. | ||
|- | |- | ||
− | |04 | + | |04:39 |
|We can have multiple ''' elsif''' conditions as per our requirement, like this. | |We can have multiple ''' elsif''' conditions as per our requirement, like this. | ||
|- | |- | ||
− | |04 | + | |04:46 |
|Here is an assignment for you - | |Here is an assignment for you - | ||
|- | |- | ||
− | |04 | + | |04:48 |
|Write an ''' if-elsif-else''' statement to print | |Write an ''' if-elsif-else''' statement to print | ||
|- | |- | ||
− | |04 | + | |04:51 |
|''' “I am a Science graduate” ''' if '''stream''' is ''' science ''' | |''' “I am a Science graduate” ''' if '''stream''' is ''' science ''' | ||
|- | |- | ||
− | |04 | + | |04:55 |
|'''“I am a Commerce graduate” ''' if '''stream''' is '''commerce''' | |'''“I am a Commerce graduate” ''' if '''stream''' is '''commerce''' | ||
|- | |- | ||
− | |04 | + | |04:59 |
|''' “I am an Arts graduate” ''' if ''' stream''' is neither ''' science''' or '''commerce ''' | |''' “I am an Arts graduate” ''' if ''' stream''' is neither ''' science''' or '''commerce ''' | ||
|- | |- | ||
− | |05 | + | |05:06 |
|Now let us learn about the '''switch''' statement. | |Now let us learn about the '''switch''' statement. | ||
|- | |- | ||
− | | 05 | + | | 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 | + | | 05:14 |
| After that, '''Switch module''' was introduced, | | After that, '''Switch module''' was introduced, | ||
|- | |- | ||
− | |05 | + | |05:18 |
|which provided the functionality of ''' switch''' statement. | |which provided the functionality of ''' switch''' statement. | ||
|- | |- | ||
− | | 05 | + | | 05:22 |
| Note: ''' Modules''' in '''Perl''' will be covered in subsequent tutorials. | | Note: ''' Modules''' in '''Perl''' will be covered in subsequent tutorials. | ||
|- | |- | ||
− | | 05 | + | | 05:27 |
| The syntax of ''' switch''' is as follows: | | The syntax of ''' switch''' is as follows: | ||
|- | |- | ||
− | | 05 | + | | 05:30 |
| use '''Switch''' semicolon | | use '''Switch''' semicolon | ||
|- | |- | ||
− | | 05 | + | | 05:32 |
| Press '''Enter''' | | Press '''Enter''' | ||
|- | |- | ||
− | | 05 | + | | 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 | + | |05:42 |
|Press '''Enter ''' | |Press '''Enter ''' | ||
|- | |- | ||
− | | 05 | + | | 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 | + | | 05:53 |
| Press '''Enter''' | | Press '''Enter''' | ||
|- | |- | ||
− | |05 | + | |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 | + | | 06:09 |
| Press ''' Enter ''' | | Press ''' Enter ''' | ||
|- | |- | ||
− | | 06 | + | | 06:10 |
|''' else''' space open curly bracket ''' executes when dollar value does not match any of the cases ''' | |''' else''' space open curly bracket ''' executes when dollar value does not match any of the cases ''' | ||
|- | |- | ||
− | |06 | + | |06:18 |
|Close curly bracket | |Close curly bracket | ||
|- | |- | ||
− | |06 | + | |06:19 |
|Press '''Enter ''' | |Press '''Enter ''' | ||
|- | |- | ||
− | | 06 | + | | 06:20 |
| close curly bracket | | close curly bracket | ||
|- | |- | ||
− | | 06 | + | | 06:22 |
| Let us understand '''switch''' using a sample program. | | Let us understand '''switch''' using a sample program. | ||
|- | |- | ||
− | | 06 | + | | 06:26 |
| Open the Terminal and type | | Open the Terminal and type | ||
|- | |- | ||
− | |06 | + | |06:29 |
| ''' gedit sampleSwitch dot pl space ampersand''' | | ''' gedit sampleSwitch dot pl space ampersand''' | ||
|- | |- | ||
− | |06 | + | |06:36 |
|and press '''Enter''' | |and press '''Enter''' | ||
|- | |- | ||
− | |06 | + | |06:38 |
|Now, type the sample program as shown on the screen. | |Now, type the sample program as shown on the screen. | ||
|- | |- | ||
− | | 06 | + | | 06:43 |
| Let us understand how the ''' switch''' statement works. | | Let us understand how the ''' switch''' statement works. | ||
|- | |- | ||
− | |06 | + | |06:46 |
| The '''use Switch''' statement includes the '''Switch''' module inside the '''Perl''' code. | | The '''use Switch''' statement includes the '''Switch''' module inside the '''Perl''' code. | ||
|- | |- | ||
− | |06 | + | |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 | + | |07:00 |
| Now we'll test the different cases. | | Now we'll test the different cases. | ||
|- | |- | ||
− | |07 | + | |07:03 |
| We have assigned ''' 'Perl' ''' to the variable ''' $var''' | | We have assigned ''' 'Perl' ''' to the variable ''' $var''' | ||
|- | |- | ||
− | |07 | + | |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 | + | |07:14 |
| In the first case, it matches with the case ''' 'Perl'.''' | | In the first case, it matches with the case ''' 'Perl'.''' | ||
|- | |- | ||
− | |07 | + | |07:19 |
| So the code written against this '''case''' will be executed. | | So the code written against this '''case''' will be executed. | ||
|- | |- | ||
− | |07 | + | |07:24 |
| Press ''' ctrl+s''' to '''save''' the file. | | Press ''' ctrl+s''' to '''save''' the file. | ||
|- | |- | ||
− | |07 | + | |07:27 |
| Now, switch to terminal and execute the script | | Now, switch to terminal and execute the script | ||
|- | |- | ||
− | |07 | + | |07:31 |
| '''perl sampleSwitch.pl''' | | '''perl sampleSwitch.pl''' | ||
|- | |- | ||
− | |07 | + | |07:36 |
| press '''Enter''' | | press '''Enter''' | ||
|- | |- | ||
− | |07 | + | |07:38 |
| The following output is shown on the terminal | | The following output is shown on the terminal | ||
|- | |- | ||
− | |07 | + | |07:41 |
|''' I am Perl ''' | |''' I am Perl ''' | ||
|- | |- | ||
− | |07 | + | |07:43 |
| Switch back to ''' sampleSwitch.pl''' in '''gedit.''' | | Switch back to ''' sampleSwitch.pl''' in '''gedit.''' | ||
|- | |- | ||
− | |07 | + | |07:48 |
| Now, let us assign ''' 'Linux' ''' to the variable ''' $var ''' | | Now, let us assign ''' 'Linux' ''' to the variable ''' $var ''' | ||
|- | |- | ||
− | |07 | + | |07:52 |
|Press ''' Ctrl S''' to save the file. | |Press ''' Ctrl S''' to save the file. | ||
|- | |- | ||
− | |07 | + | |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 | + | |08:03 |
| It matches with the case ''' 'Linux' ''' | | It matches with the case ''' 'Linux' ''' | ||
|- | |- | ||
− | |08 | + | |08:05 |
| So code written against this case will get executed. | | So code written against this case will get executed. | ||
|- | |- | ||
− | |08 | + | |08:10 |
| Now, switch to terminal and execute the script | | Now, switch to terminal and execute the script | ||
|- | |- | ||
− | |08 | + | |08:15 |
| '''perl sampleSwitch.pl''' | | '''perl sampleSwitch.pl''' | ||
|- | |- | ||
− | |08 | + | |08:19 |
| Press '''Enter''' | | Press '''Enter''' | ||
|- | |- | ||
− | |08 | + | |08:21 |
| The following output is shown on the terminal | | The following output is shown on the terminal | ||
|- | |- | ||
− | |08 | + | |08:24 |
| ''' I am Linux''' | | ''' I am Linux''' | ||
− | |||
|- | |- | ||
− | |08 | + | |08:26 |
| Switch to ''' sampleSwitch.pl''' in ''' gedit.''' | | Switch to ''' sampleSwitch.pl''' in ''' gedit.''' | ||
|- | |- | ||
− | |08 | + | |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 | + | |08:38 |
| Now, let us assign ''' 'English' ''' to the variable ''' $var''' | | Now, let us assign ''' 'English' ''' to the variable ''' $var''' | ||
|- | |- | ||
− | |08 | + | |08:42 |
| Again, the value of variable ''' $var ''' will be checked in switch. | | Again, the value of variable ''' $var ''' will be checked in switch. | ||
|- | |- | ||
− | |08 | + | |08:47 |
|It does not match any of the ''' case''' statements. | |It does not match any of the ''' case''' statements. | ||
|- | |- | ||
− | |08 | + | |08:50 |
| So the ''' else ''' statement will be executed. | | So the ''' else ''' statement will be executed. | ||
|- | |- | ||
− | |08 | + | |08:54 |
| Now, switch to terminal and execute the script | | Now, switch to terminal and execute the script | ||
|- | |- | ||
− | |09 | + | |09:00 |
| ''' perl sampleSwitch.pl''' | | ''' perl sampleSwitch.pl''' | ||
|- | |- | ||
− | |09 | + | |09:07 |
| and Press ''' Enter''' | | and Press ''' Enter''' | ||
|- | |- | ||
− | |09 | + | |09:09 |
| The following output is shown on the terminal - | | The following output is shown on the terminal - | ||
|- | |- | ||
− | |09 | + | |09:12 |
|''' I am not a computer language ''' | |''' I am not a computer language ''' | ||
|- | |- | ||
− | |09 | + | |09:17 |
| The 3 cases imply that: | | The 3 cases imply that: | ||
|- | |- | ||
− | |09 | + | |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 | + | |09:25 |
| Only the valid ''' case''' will be executed and | | Only the valid ''' case''' will be executed and | ||
|- | |- | ||
− | |09 | + | |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 | + | |09:35 |
| It is not mandatory to write the '''else case. ''' | | It is not mandatory to write the '''else case. ''' | ||
|- | |- | ||
− | |09 | + | |09:39 |
| In such a scenario, | | In such a scenario, | ||
|- | |- | ||
− | |09 | + | |09:41 |
| if none of the ''' cases''' match | | if none of the ''' cases''' match | ||
|- | |- | ||
− | |09 | + | |09:44 |
| then there will be no output from the ''' switch''' statement. | | then there will be no output from the ''' switch''' statement. | ||
|- | |- | ||
− | |09 | + | |09:48 |
| Here is another assignment for you - | | Here is another assignment for you - | ||
|- | |- | ||
− | |09 | + | |09:50 |
| Re-write the previous assignment | | Re-write the previous assignment | ||
|- | |- | ||
− | |09 | + | |09:53 |
| given earlier in this tutorial using ''' switch''' statement. | | given earlier in this tutorial using ''' switch''' statement. | ||
− | |||
|- | |- | ||
− | |09 | + | |09:57 |
| Let us summarize. | | Let us summarize. | ||
|- | |- | ||
− | |09 | + | |09:59 |
| In this tutorial, we have learnt - | | In this tutorial, we have learnt - | ||
− | |||
|- | |- | ||
− | |10 | + | |10:01 |
| ''' if-elsif-else''' statement and | | ''' if-elsif-else''' statement and | ||
− | |||
− | |||
|- | |- | ||
− | |10 | + | |10:04 |
| '''switch''' statement in '''Perl ''' | | '''switch''' statement in '''Perl ''' | ||
|- | |- | ||
− | |10 | + | |10:05 |
| '''using sample programs. ''' | | '''using sample programs. ''' | ||
|- | |- | ||
− | |10 | + | |10:08 |
|Watch the video available at the following link | |Watch the video available at the following link | ||
|- | |- | ||
− | |10 | + | |10:12 |
|It summaries the Spoken Tutorial project | |It summaries the Spoken Tutorial project | ||
− | |||
|- | |- | ||
− | |10 | + | |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 | + | | 10:20 |
| The Spoken Tutorial Project Team Conducts workshops using spoken tutorials | | The Spoken Tutorial Project Team Conducts workshops using spoken tutorials | ||
− | |||
|- | |- | ||
− | |10 | + | |10:25 |
|Gives certificates to those who pass an online test | |Gives certificates to those who pass an online test | ||
− | |||
|- | |- | ||
− | |10 | + | |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 | + | | 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 | ||
− | |||
|- | |- | ||
− | |10 | + | |10:40 |
|It is supported by the National Mission on Education through ICT, MHRD, Government of India. | |It is supported by the National Mission on Education through ICT, MHRD, Government of India. | ||
− | |||
|- | |- | ||
− | |10 | + | |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 | + | | 10:58 |
| Hope you enjoyed this Perl tutorial. | | Hope you enjoyed this Perl tutorial. | ||
|- | |- | ||
− | |11 | + | |11:00 |
|This is Amol signing off. | |This is Amol signing off. | ||
− | |||
|- | |- | ||
− | |11 | + | |11:03 |
|Thanks for joining. | |Thanks for joining. | ||
|} | |} |
Revision as of 10:49, 10 July 2014
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. |