PERL/C2/while-do-while-loops/English
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.
|
|
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
|
Slide: Prerequisites | You should have basic knowledge of Variables and Comments in Perl
|
Slide | while loop in Perl
|
Slide
Piece of code to be executed while the condition is true } |
The syntax of while loop is as follows -
|
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
|
Open the Terminal and type
|
Point to the filename whileLoop.pl in the Titlebar of gedit.
while ($i<=4) { print “Value of i: $i\n”; $i++; }
|
This will open the whileLoop.pl file in gedit.
Press Enter
Press Enter
press enter and Close the curly bracket |
Highlight $i=0; | Let me explain the while loop in detail.
|
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.
| |
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 -
|
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
|
As there are no compilation or syntax error, we will execute the Perl script by typing -
|
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.
|
Slide
Piece of code to be executed while the condition is true } while (condition); |
The syntax for do-while loop is as follows -
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
|
Open the Terminal and type;
|
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
print “Value of i: $i\n”; $i++; } while ($i<=4);
|
Type the following piece of code -
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
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'
|
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.
| |
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
|
Now, switch to terminal and type the following to check for any compilation or syntax error.
|
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
|
As there are no compilation or syntax errors, we will now execute the Perl script.
|
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
|
Open the Terminal and type -
|
#!/usr/bin/perl
# while loop while ($count > 0) { print “I am in while loop\n”; }
do { print “I am in do-while loop\n”; } while ($count > 0); |
This will open loop dot pl file in gedit.
|
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.
|
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
|
Now, switch to terminal and type the following to check for any compilation or syntax errors
|
Highlight the below line on terminal;
loop.pl syntax OK |
The following line will be displayed on the terminal
|
Execute perl script
|
As there are no compilation or syntax errors, let us execute the Perl script.
By Typing
|
Highlight the output of the perl script on terminal
|
The following output will be displayed on the terminal.
|
Here we can see, there is no output message displaying 'I am in while loop'
| |
Slide | This implies that,
|
I hope the difference is clear to you now.
| |
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 -
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
download and watch it |
Spoken Tutorial Workshops | The Spoken Tutorial Project Team
test
contact at spoken hyphen tutorial dot org |
Acknowledgment | Spoken Tutorial Project is a part of the Talk to a
Teacher project
Education through ICT, MHRD, Government of India.
spoken hypen tutorial dot org slash NMEICT hyphen Intro |
Hope you enjoyed this Perl tutorial.
This is Amol Brahmankar signing off.
|