PERL/C2/while-do-while-loops/English

From Script | Spoken-Tutorial
Revision as of 16:41, 23 March 2013 by AmolBrahmankar (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Title Of Script: while and do-while loops in Perl

Author: Amol Brahmankar

Keywords: Loops in Perl, while loop in perl, do-while loop in perl, video tutorial.


Visual Clue
Narration
Slide Welcome to the spoken tutorial on while and do-while loops in Perl
Slide: Learning Objectives In this tutorial, we will learn about;

while loop in Perl

do-while 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


Knowledge of for and foreach loops in Perl will be an added advantage.


Please go through the relevant spoken tutorials on the spoken tutorial website.

Slide while loop in Perl


The while loop executes a block of code while a condition is true.

Slide


while (condition) {

Piece of code to be executed while the condition is true

}

The syntax of while loop is as follows -


while space open bracket condition close bracket


Open curly bracket


Piece of code to be executed while the condition is true


Close curly bracket

Highlight while (condition) So, what happens if the condition is not satisfied? Then, the while loop will exit, without executing the code within, even once.
Now let us look at an example of while loop.
Switch to the Terminal and type


gedit whileLoop.pl &


and press Enter.

Open the Terminal and type


gedit whileLoop dot pl space ampersand


and press Enter

Point to the filename whileLoop.pl in the Titlebar of gedit.


Type the piece of code.


#!/usr/bin/perl


$i = 0;

while ($i<=4) {

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

$i++;

}


This will open the whileLoop.pl file in gedit.


Now Type the following code


hash exclamation mark slash u s r slash bin slash perl


Press Enter.


dollar i is equal to zero semicolon

Press Enter


while open bracket dollar i less than or equal to four close bracket space


Open curly bracket press enter and type


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

Press Enter


dollar i plus plus semicolon

press enter and

Close the curly bracket

Highlight $i=0; Let me explain the while loop in detail.


We have initialized the variable i to 0.

Now we have specified the condition for while loop as $i less than or equal to 4.

If the condition is true, the code within the while loop will get executed.

Highlight print This means, first time our while loop will print 'Value of i: 0' on the terminal.
Highlight $i++


Then the $i++ increments the value of variable i by one.
Now again, the loop condition $i<=4 will be evaluated.


And the loop will exit once the value of i becomes 5.

In this case, while loop will get executed for i equal to 0, 1, 2, 3, 4.
Press Ctrl S Press ctrl+s to save the file.
Switch to terminal Now, switch to the terminal.
perl -c whileLoop.pl


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


perl hyphen c whileLoop dot pl


and press Enter.

Highlight the below line on terminal-

whileLoop.pl syntax OK

The following line will be displayed on the terminal

whileLoop.pl syntax OK

Execute Perl script


perl whileLoop.pl

As there are no compilation or syntax error, we will execute the Perl script by typing -


perl whileLoop dot pl


and press Enter

Highlight 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 displayed on the terminal.



Now, let us look at do-while loop
Slide The do...while statement will always execute the piece of code at-least once.


It will then check the condition and repeat the loop while the condition is true.

Slide


do {

Piece of code to be executed while the condition is true

} while (condition);

The syntax for do-while loop is as follows -


do space

open curly bracket

Piece of code to be executed while the condition is true

close curly bracket then space

while space within brackets condition and then semicolon



Switch to the Terminal and type


gedit doWhileLoop.pl &


and press Enter.

Open the Terminal and type;


gedit doWhileLoop dot pl space ampersand


and then press Enter

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


#!/usr/bin/perl


$i = 0;


do {

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

$i++;

} while ($i<=4);


Type the following piece of code -


hash exclamation mark slash u s r slash bin slash perl


Press Enter


dollar i equals to zero semicolon press enter

do space

open curly bracket enter type

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

Press Enter


dollar i plus plus semicolon

press enter

close curly bracket

space while space open bracket dollar i less than or equal to four

close bracket semicolon

Highlight $i=0; Here is the detail explanation of a do-while loop.

We have initialized the variable i to 0.

Highlight print First time, the do-while loop will print the output as 'Value of i colon 0'


without checking for condition

Highlight $i++; $i++ will increment the value of variable i by one each time loop gets executed.
Second time, the condition $i less than or equal to 4 will be checked.


If the condition is true, the loop will get executed again.


In our case, second time the output displayed on terminal will be 'Value of I colon 1'.

The loop will get executed till the condition becomes false that is when variable i becomes 5.
Press Ctrl+S Press ctrl+s to save the file.
Switch to terminal


perl -c doWhileLoop.pl


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


perl hyphen c doWhileLoop dot pl


and press Enter.

Highlight the below line on terminal -

doWhileLoop.pl syntax OK

The following line will be displayed on the terminal

doWhileLoop.pl syntax OK

Execute Perl script


perl doWhileLoop.pl

As there are no compilation or syntax errors, we will now execute the Perl script.


Type


perl doWhileLoop dot pl


and press Enter

Highlight 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 displayed on the terminal.



Now, let us see the actual difference between while and do-while loops.
Switch to the Terminal and type


gedit loop.pl &


and press Enter.

Open the Terminal and type -


gedit loop dot pl space ampersand


and press Enter

#!/usr/bin/perl


$count = 0;

# while loop

while ($count > 0) {

print “I am in while loop\n”;

}


# do-while loop

do {

print “I am in do-while loop\n”;

} while ($count > 0);

This will open loop dot pl file in gedit.


Now type the piece of code shown.



Highlight

$count = 0;

We have declared a variable count and initialized it to zero
Highlight

while ($count > 0)

In the while loop condition, we are checking if the variable count is greater than zero.


The condition is not true. So, the while loop code will not be executed even once.

Highlight

do {


In the do...while loop, we are first executing the code and then checking the condition.
Highlight

print “I am in do-while loop\n”;


So, the code will be executed at least once.
Highlight

} while ($count > 0);

Then the condition whether the variable count is greater than zero, is checked.
The condition is not true. So the loop will exit.
Press Ctrl S Now, press ctrl+s to save the file.
Switch to terminal


type perl -c loop.pl


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


perl hyphen c loop dot pl


and press Enter.

Highlight the below line on terminal;

loop.pl syntax OK

The following line will be displayed on the terminal


loop dot pl syntax OK

Execute perl script


perl loop.pl

As there are no compilation or syntax errors, let us execute the Perl script.

By Typing


perl loop dot pl


and press Enter.

Highlight the output of the perl script on terminal


I am in do-while loop

The following output will be displayed on the terminal.


I am in do-while loop

Here we can see, there is no output message displaying 'I am in while loop'


This message was what we printed inside the while loop.

Slide This implies that,


do-while loop executes at-least once before evaluating the condition


whereas while loop does not get executed even once when the condition specified is false.

I hope the difference is clear to you now.


That's all there is to while and do-while loops.

Summary Let us summarize.

In this tutorial, we have learnt -

while loop and

do-while loop in Perl

using sample programs.

Assignment Here is assignment for you -


Print 'Hello Perl'

till the count of variable reaches 10

by using while loop and do-while loops

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