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

From Script | Spoken-Tutorial
Jump to: navigation, search
 
Line 9: Line 9:
 
|-
 
|-
 
| 00:07
 
| 00:07
| In this tutorial, we will learn about  
+
|In this tutorial, we will learn about  
  
 
|-
 
|-
Line 77: Line 77:
 
|-
 
|-
 
|01:30
 
|01:30
|press '''Enter '''.
+
|press '''Enter ''', another piece of code semicolon  
 
+
|-
+
|01:31
+
|another piece of code semicolon  
+
 
   
 
   
 
|-
 
|-
Line 97: Line 93:
 
|-
 
|-
 
|01:44
 
|01:44
|'''Enter'''  
+
|'''Enter'''. Code to be executed when both the above conditions are '''false''' semicolon
+
|-
+
| 01:45
+
|code 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.  
+
  
 
|-
 
|-
Line 157: Line 145:
 
|-
 
|-
 
|02:49
 
|02:49
| And then we have various conditions that we want to check.
+
|And then we have various conditions that we want to check.
 
   
 
   
 
|-
 
|-
Line 169: Line 157:
 
|-
 
|-
 
| 03:02
 
| 03:02
| Type: '''perl conditionalBlocks dot pl '''
+
|Type: '''perl conditionalBlocks dot pl '''
 
   
 
   
 
|-
 
|-
Line 176: 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.
 
   
 
   
Line 208: Line 196:
 
   
 
   
 
|-
 
|-
| 03:37
+
|03:37
| Press '''Ctrl+s''' to '''save''' the file.  
+
| Press '''Ctrl+s''' to '''save''' the file.  
 
   
 
   
 
|-
 
|-
Line 224: Line 212:
 
   
 
   
 
|-
 
|-
|   03:53
+
|03:53
 
|The output shown on the terminal is "Hi, I am Java".
 
|The output shown on the terminal is "Hi, I am Java".
 
   
 
   
Line 249: Line 237:
 
|-
 
|-
 
|04:18
 
|04:18
|and press '''Enter.'''
+
|and press '''Enter.''' The output shown on the terminal is:  
+
|-
+
|04:19
+
|The output shown on the terminal is:  
+
  
 
|-
 
|-
Line 261: Line 245:
 
|-
 
|-
 
| 04:27
 
| 04:27
| The three cases imply that  
+
|The three cases imply that  
  
 
|-
 
|-
Line 300: Line 284:
 
   
 
   
 
|-
 
|-
| 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 349: Line 333:
 
|-
 
|-
 
| 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 '''.
+
  
 
|-
 
|-
Line 585: Line 561:
 
|-
 
|-
 
|10:01
 
|10:01
|* ''' if-elsif-else''' statement and  
+
| ''' if-elsif-else''' statement and  
  
 
|-
 
|-
 
|10:04
 
|10:04
|* '''switch''' statement in '''Perl '''
+
| '''switch''' statement in '''Perl ''' using sample programs.  
 
+
|-
+
|10:05
+
| using sample programs.  
+
  
 
|-
 
|-
Line 609: Line 581:
 
|-
 
|-
 
| 10:20
 
| 10:20
| The Spoken Tutorial project team:  Conducts workshops using spoken tutorials.
+
|The Spoken Tutorial project team:  Conducts workshops using spoken tutorials.
 
   
 
   
 
|-
 
|-
Line 621: Line 593:
 
|-
 
|-
 
| 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 629: Line 601:
 
|-
 
|-
 
|10:47
 
|10:47
|More information on this mission is available at:
+
|More information on this mission is available at: spoken hyphen tutorial dot org slash NMEICT hyphen Intro.
spoken hyphen tutorial dot org slash NMEICT hyphen Intro.
+
 
   
 
   
 
|-
 
|-

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