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

From Script | Spoken-Tutorial
Jump to: navigation, search
 
Line 8: Line 8:
  
 
{| style="border-spacing:0;"
 
{| style="border-spacing:0;"
| style="border-top:0.05pt solid #000000;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <center>'''Visual Clue'''</center>
+
| style="border-top:0.05pt solid #000000;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <center>'''Visual Cue'''</center>
 
| style="border:0.05pt solid #000000;padding:0.097cm;"| <center>'''Narration'''</center>
 
| style="border:0.05pt solid #000000;padding:0.097cm;"| <center>'''Narration'''</center>
  

Latest revision as of 12:58, 17 June 2014

Title Of Script: for & foreach Loops in Perl

Author: Amol Brahmankar

Keywords: Loops in Perl, for loop in perl, foreach in perl, video tutorial.


Visual Cue
Narration
Slide Welcome to the spoken tutorial on for and foreach Loops in Perl
Slide: Learning Objectives In this tutorial, we will learn about:

for loop in Perl and

foreach loop in Perl

Slide: System Requirements I am using Ubuntu Linux12.04 operating system and Perl 5.14.2


I will also be using the gedit Text Editor.


You can use any text editor of your choice.

Slide: Prerequisites You should have basic knowledge of Variables and Comments in Perl.


If not, please go through the relevant spoken tutorials on the spoken tutorial website.

Slide Perl provides a mechanism by which we can check a condition repeatedly for various values. This is done using loops.
Slide There are various types of loops in Perl;

for loop

foreach loop

while loop &

do-while loop


In this tutorial, we'll cover for and foreach loop.

Slide for loop in Perl can be used to execute a piece of code for a certain number of times.
Slide


for (variable initialization; condition; increment/decrement)

{

Piece of code to be executed multiple times

}

The syntax of for loop is as follows:


for space open bracket variable initialization semicolon condition semicolon increment close bracket


Press Enter


Open curly brackets


Piece of code to be executed multiple times


Close curly brackets



Now let us look at an example of a for loop.
Switch to the Terminal and type

gedit forLoop.pl &

and press Enter.

Open the Terminal and type;

gedit forLoop.pl space & (ampersand)

and press Enter

Point to the filename forLoop.pl in the Titlebar of gedit. This will open the forLoop.pl file in gedit.
Type the piece of code and save the file


#!/usr/bin/perl


for ($i=0; $i<=4; $i++) {

print “Value of i: $i\n”;

}

Type the following piece of code;


hash exclamation mark slash u s r slash bin slash perl


Press Enter


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

space

Open curly bracket

press enter , type

print space double quote Value of i colon space dollar I slash n double quote complete semicolon


And Press Enter

now

ANd Close curly bracket



Press ctrl+s Press Ctrl+S to save the file.
Highlight $i=0 Let me explain what the for loop does.


The variable i is initialized to zero.

Highlight $i<=4 Next, the condition is checked.

In this case, the condition is i is less than or equal to 4.


If this condition is true, the code within the curly bracket will be executed.

Highlight print This means the first print statement

“Value of i: 0”

will be displayed on the terminal.

Highlight $i++ After this, the variable i is incremented by 1.
Highlight $i<=4 And the for loop condition is checked once again.


This loop will exit when the value of i becomes greater than 4.

In this case, the for loop will be executed for i = 0, 1, 2, 3, 4


which is a total of 5 times.

Switch to terminal Now, switch to the terminal.
perl -c forLoop.pl


Type the following to check for any compilation or syntax error:


perl hyphen c forLoop dot pl


and press Enter.

Highlight the below line on terminal;

forLoop.pl syntax OK

Here it displays a message

forLoop.pl syntax OK


So, we have no errors.

Execute perl script


perl forLoop.pl

Now, let's execute the Perl script by typing


perl forLoop dot pl


and press Enter.

Highlight/point to the output of the perl script on terminal

Value of i: 0

Value of i: 1

Value of i: 2

Value of i: 3

Value of i: 4

The following output will be shown on terminal.



Now, let us look at the foreach loop.
Slide If we want to iterate a condition for an array, we can make use of foreach loop.
Slide


foreach $variable (@array) {

perform action on each element of an array

}

The syntax is:


foreach space dollar variable space within brackets at the rate array


space


open curly bracket


perform action on each element of an array


Press Enter

Close the curly bracket.


Please note: We'll cover array, array initialization and defining an array in subsequent tutorials.

Now let us look at an example of foreach loop.
Switch to the Terminal and type


gedit foreachLoop.pl &


and press Enter.

Open the Terminal and type


gedit foreachLoop dot pl space ampersand


and Press Enter

Point to the filename foreachLoop.pl in the Titlebar of gedit. This will open the foreachLoop.pl file in gedit.
#!/usr/bin/perl


@myarray = (10, 20, 30);


foreach $var (@myarray) {

print “Element of array is: $var \n”;

}


Type the following piece of code


hash exclamation mark slash u s r slash bin slash perl


and Press Enter


at the rate myarray is equal to open bracket ten comma space twenty comma space thirty close bracket semicolon

press enter

foreach space dollar var space open bracket at the rate myarray close bracket space


Open curly bracket

press enter and type


print space double quote Element of array is colon dollar var space slash n double quote complete semicolon


Press Enter and

Close curly bracket

Press Ctrl+S Press ctrl+s to save the file.
Highlight

@myarray = (10, 20, 30);

Let me explain what this code does.


An array myarray is declared.

It has 3 elements 10, 20 and 30.

Highlight $var In each iteration of foreach loop dollar var will contain the single element of an array
Highlight foreach $var (@myarray) foreach keyword will repeat this loop for each element of an array.
Highlight print “Element of array is:$var \n”; That is, the code within the curly bracket will be executed for each myarray element.
Highlight \n Back-slash n will place the prompt on a new line.
This means the first element '10' will be displayed on the terminal.


Then 20 and so on, till all the elements are printed.

Highlight foreach This loop will exit after printing all the elements in myarray.
Switch to terminal


perl -c foreachLoop.pl


Now, switch to terminal and type the following to check for any compilation or syntax error.

type

perl hyphen c foreachLoop dot pl


and press Enter.

Highlight the below line on terminal;

foreachLoop.pl syntax OK

The following line will be shown on terminal
Execute perl script


perl foreachLoop.pl

There are no compilation or syntax errors.

So let us execute the Perl script. Type


perl foreachLoop dot pl


and press Enter

Highlight the output of the perl script on terminal

Element of an array is: 10

Element of an array is: 20

Element of an array is: 30

The following output will be shown on terminal.



So, that's it about for loop and foreach loop.
Slide : Summary Let us summarize.

In this tutorial, we have learnt -

for loop and

foreach loop in Perl

using some sample programs.

Assignment Here is an assignment for you -
  1. Declare a string as 'Spoken Tutorial' and
  2. Print it 5 times
  3. Declare an array of colours as @colorArray = ('red', 'white', 'blue') and
  4. Print the element of an array using foreach loop


About the Project Watch the video available at the following link


It summaries the Spoken Tutorial project


If you do not have good bandwidth, you can

download and watch it

Spoken Tutorial Workshops The Spoken Tutorial Project Team


Conducts workshops using spoken tutorials


Gives certificates to those who pass an online

test


For more details, please write to

contact at spoken hyphen tutorial dot org

Acknowledgment


http://spoken-tutorial.org\NMEICT-Intro

Spoken Tutorial Project is a part of the Talk to a

Teacher project


It is supported by the National Mission on

Education through ICT, MHRD, Government of India.


More information on this Mission is available


spoken hypen tutorial dot org slash NMEICT hyphen Intro

Hope you enjoyed this Perl tutorial.

This is Amol Brahmankar signing off.


Thanks for joining.

Contributors and Content Editors

AmolBrahmankar, PoojaMoolya, Sneha