Difference between revisions of "PERL/C2/for-for-each-loops/English-timed"

From Script | Spoken-Tutorial
Jump to: navigation, search
Line 404: Line 404:
 
|-
 
|-
 
|08:40
 
|08:40
|It summaries the Spoken Tutorial project.
+
|It summarizes the Spoken Tutorial project.
 
   
 
   
 
|-
 
|-

Revision as of 10:04, 8 May 2015

Time Narration
00:01 Welcome to the spoken tutorial on for and foreach Loops in Perl.
00:06 In this tutorial, we will learn about: for loop in Perl and
00:11 foreach loop in Perl.
00:13 I am using Ubuntu Linux 12.04 operating system and Perl 5.14.2.
00:21 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:33 If not, please go through the relevant spoken tutorials on the spoken tutorial website.
00:40 Perl provides a mechanism by which we can check a condition repeatedly for various values. This is done using loops.
00:49 There are various types of loops in Perl:
00:52 * for loop
00:53 * foreach loop
00:54 * while loop and
00:55 * do-while loop.
00:56 In this tutorial, we will cover for and foreach loop.
01:01 for loop in Perl can be used to execute a piece of code for a certain number of times.
01:07 The syntax of for loop is as follows:
01:10 for space open bracket variable initialization semicolon condition semicolon increment
01:20 close bracket, press Enter.
01:22 Open curly brackets,
01:24 piece of code to be executed multiple times,
01:28 close curly brackets.
01:30 Now let us look at an example of a for loop.
01:33 Open the Terminal and type: gedit forLoop.pl space & (ampersand)
01:42 and press Enter.
01:43 This will open the forLoop.pl file in gedit.
1:48 Type the following piece of code: hash exclamation mark slash u s r slash bin slash perl
01:58 press Enter.
02:00 for space open bracket dollar i equals to zero semicolon space dollar i less than or equal to four semicolon space dollar i plus plus close bracket
02:18 space
02:19 Open curly bracket press Enter.
02:21 Type: print space double quote Value of i colon <space> dollar i slash n double quote complete semicolon
02:35 and press Enter.
02:36 Now close curly bracket.
02:39 Press Ctrl+S to save the file.
02:42 Let me explain what the for loop does.
02:46 The variable i is initialized to zero.
02:50 Next, the condition is checked.
02:53 In this case, the condition is i less than or equal to 4.
02:59 If this condition is true, the code within the curly bracket will be executed.
03:05 This means the first print statement "Value of i colon 0"
03:11 will be displayed on the terminal.
03:14 After this, the variable 'i' is incremented by 1.
03:18 And the for loop condition is checked once again.
03:23 This loop will exit when the value of ' i' becomes greater than 4.
03:29 In this case, the for loop will be executed for i = 0, 1, 2, 3, and 4.
03:38 which is a total of 5 times.
03:41 Now, switch to terminal.
03:44 Type the following to check for any compilation or syntax error:
03:48 perl hyphen c forLoop dot pl
03:54 and press Enter.
03:56 Here it displays a message:
03:58 forLoop.pl syntax OK.
04:01 So, we have no errors.
04:03 Now, let us execute the Perl script by typing perl forLoop dot pl and press Enter.
04:11 The following output will be shown on the terminal.
04:16 Now, let us look at foreach loop.
04:19 If we want to iterate a condition for an array, we can make use of foreach loop.
04:25 The syntax is: foreach space dollar variable space within brackets at the rate array space
04:35 open curly bracket
04:37 perform action on each element of an array press Enter,
04:42 close the curly bracket.
04:44 Please note: We'll cover array, array initialization and defining an array in subsequent tutorials.
04:52 Now let us look at an example of foreach loop.
04:56 Open the Terminal and type: gedit foreachLoop dot pl space ampersand and Press Enter.
05:08 This will open the foreachLoop.pl file in gedit.
05:12 Type the following piece of code:
05:15 hash exclamation mark slash u s r slash bin slash perl and press Enter.
05:25 at the rate myarray space equal to space open bracket ten comma twenty comma thirty close the bracket semicolon
05:39 press Enter.
05:41 foreach space dollar var space open bracket at the rate myarray close the bracket space
05:52 open curly bracket press Enter and type:
05:56 print space double quote Element of an array is colon dollar var slash n double quote complete semicolon
06:13 press Enter and close the curly bracket.
06:17 Press Ctrl+s to save the file.
06:20 Let me explain what this code does. An array myarray is declared.
06:27 It has 3 elements 10, 20 and 30.
06:33 In each iteration of foreach loop, 'dollar var' ($var) will contain the single element of an array.
06:40 foreach keyword will repeat this loop for each element of an array.
06:47 That is, the code within the curly bracket will be executed for each myarray element.
06:55 Back-slash n (\n) will place the prompt on a new line.
07:00 This means, the first element 10 will be displayed on the terminal.
07:06 Then 20 and so on, till all the elements are printed.
07:12 This loop will exit after printing all the elements in myarray.
07:17 Now, switch to terminal and type the following to check for any compilation or syntax error.
07:24 Type: perl hyphen c foreachLoop dot pl and press Enter.
07:32 The following line will be shown on the terminal.
07:36 There are no compilation or syntax errors.
07:38 So, let us execute the Perl script.
07:41 Type: perl foreachLoop dot pl and press Enter.
07:48 The following output will be shown on the terminal.
07:54 So, that's it about for loop and foreach loop.
07:57 Let us summarize.
07:59 In this tutorial, we have learnt -
08:02 for loop and foreach loop in Perl
08:06 using some sample programs.
08:07 Here is an assignment for you -
08:10 Declare a string as 'Spoken Tutorial' and
08:13 print it 5 times.
08:16 Declare an array of colors as @colorArray = open bracket in single quote red comma white comma blue close the bracket and
08:32 print the element of an array using foreach loop.
08:36 Watch the video available at the following link.
08:40 It summarizes the Spoken Tutorial project.
08:43 If you do not have good bandwidth, you can download and watch it.
08:48 The Spoken Tutorial Project Team: Conducts workshops using spoken tutorials.
08:55 Gives certificates to those who pass an online test.
08:59 For more details, please write to contact at spoken hyphen tutorial dot org.
09:07 "Spoken Tutorial" project is a part of the "Talk to a Teacher" project.
09:12 It is supported by the National Mission on Education through ICT, MHRD, Government of India.
09:20 More information on this mission is available at spoken hyphen tutorial dot org slash NMEICT hyphen Intro.
09:31 Hope you enjoyed this Perl tutorial.
09:34 This is Amol, signing off.
09:36 Thanks for joining.

Contributors and Content Editors

Gaurav, Madhurig, PoojaMoolya, Sandhya.np14, Sneha