PERL/C2/while-do-while-loops/English-timed
{| border=1
!|- | 00.01 || Welcome to the spoken tutorial on while and do-while loops in Perl
|- | 00.07 |In this tutorial, we will learn about;while loop in Perl
|- | 00.11 |do-while loop in Perl
|- | 00.13 | I am using Ubuntu Linux12.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
|Knowledge of for and foreach loops in Perl will be an added advantage.
|-
| 00.39
|Please go through the relevant spoken tutorials on the spoken tutorial website.
|- | 00.44 | while loop in Perl
|-
| 00.46
|The while loop executes a block of code while a condition is true.
|- | 00.50 | The syntax of while loop is as follows -
|-
| 00.53
|while space open bracket condition close bracket Open curly bracket
|-
|01.00
|Piece of code to be executed while the condition is true. Close curly bracket
|- | 01.07 | So, what happens if the condition is not satisfied? Then, the while loop will exit, without executing the code within, even once.
|- | 01.17 | Now let us look at an example of while loop.
|- | 01.20 | 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++; }
|- | 01.31 |This will open the whileLoop.pl file in gedit.
|-
| 01.35
|Now Type the following code
|-
| 01.37
|hash exclamation mark slash u s r slash bin slash perl Press Enter.
|-
| 01.47
|dollar i is equal to zero semicolon
|- |01.52 |Press Enter
|-
| 01.54
1. |While open bracket dollar i less than or equal to four close bracket space
|-
| 02.04
|Open curly bracket press enter and type
|-
| 02.08
|print space double quote Value of i colon dollar i slash n double quote complete semicolon
|- | 02.21 |Press Enter
|-
| 02.23
|dollar i plus plus semicolon press enter and
|- | 02.29 |Close the curly bracket
|- | 02.31 | Let me explain the while loop in detail. |- | 02.34 |We have initialized the variable i to 0.
|- | 02.39 | Now we have specified the condition for while loop as $i less than or equal to 4.
|- | 02.47 |If the condition is true, the code within the while loop will get executed.
|- | 02.52 | This means, first time our while loop will print 'Value of i: 0' on the terminal.
|- | 03.01 | Then the $i++will increments the value of variable i by one.
|- |03.08 | Now again, the loop condition $i<=4 will be evaluated.
|-
| 03.17
|And the loop will exit once the value of i becomes 5.
|- |03.22 | In this case, while loop will get executed for i equal to 0, 1, 2, 3, 4.
|- |03.32 |Press ctrl+s to save the file.
|- |03.35 |Now, switch to the terminal.
|- | 03.38 | Type the following to check for any compilation or syntax error -
|-
| 03.43
|perl hyphen c whileLoop dot pl and press Enter.
|- | 03.49 | The following line will be displayed on the terminal whileLoop.pl syntax OK
|- | 03.56 | As there is no compilation or syntax error, we will execute the Perl script by typing -
|-
| 04.03
|perl whileLoop dot pl and press Enter
|- | 04.10 | The following output will be displayed on the terminal.
|- | 04.15 |Now, let us look at do-while loop
|- | 04.20 | The do...while statement will always execute the piece of code at-least once.
|-
| 04.26
|It will then check the condition and repeat the loop while the condition is true.
|- | 04.31 | The syntax for do-while loop is as follows - do space
|- | 04.36 |open curly bracket
|- | 04.38 |Piece of code to be executed while the condition is true
|- | 04.42 |close curly bracket then space
|- | 04.45 |while space within brackets condition and then semicolon
|- | 04.51 | Open the Terminal and type;
|-
| 04.55
|gedit doWhileLoop dot pl space ampersand
|-
| 05.03
|and then press Enter
|- | 05.05 | This will open the doWhileLoop.pl file in gedit.
|- | 05.09 |Type the following piece of code -
|-
| 05.12
|hash exclamation mark slash u s r slash bin slash perl
|-
| 05.20
|Press Enter
|-
| 05.21
|dollar i equals to zero semicolon press enter
|- | 05.28 |do space
|- | 05.30 | open curly bracket enter type
|- | 05.33 |print space double quote Value of i colon space dollar i slash n double quote complete semicolon
|- | 05.47 |Press Enter
|-
| 05.49
|dollar i plus plus semicolon press enter
|- | 05.54 |close curly bracket
|- | 05.56 |space while space open bracket dollar i less than or equal to four
|- | 06.06 |close bracket semicolon
|- |06.11 | Here is the detail explanation of a do-while loop.
|- | 06.14 |We have initialized the variable i to 0.
|- | 06.19 | First time, the do-while loop will print the output as 'Value of i colon 0' on the terminal
|-
| 06.26
|without checking for condition
|- | 06.29 | $i++ will increment the value of variable i by one each time loop gets executed.
|- | 06.37 | Second time, the condition $i less than or equal to 4 will be checked.
|-
| 06.44
|If the condition is true, the loop will get executed again.
|-
| 06.49
|In our case, second time the output displayed on terminal will be 'Value of I colon 1'.
|- | 06.58 | The loop will get executed till the condition becomes false that is when variable i becomes 5.
|- | 07.07 | Press ctrl+s to save the file.
|- | 07.09 | Now, switch to terminal and type the following to check for any compilation or syntax error.
|-
| 07.16
|perl hyphen c doWhileLoop dot pl and press Enter.
|- | 07.24 | The following line will be displayed on the terminal
|- | 07.27 |doWhileLoop.pl syntax OK
|- | 07.31 | As there are no compilation or syntax errors, we will now execute the Perl script.
|-
| 07.36
|Type,perl doWhileLoop dot pl and press Enter
|- | 07.44 | The following output will be displayed on the terminal.
|- |07.48 |Now, let us see the actual difference between while and do-while loops.
|- | 07.53 | Open the Terminal and type -
|-
| 07.56
|gedit loop dot pl space ampersand and press Enter
|- | 08.03 | This will open loop dot pl file in gedit.
|-
| 08.08
|Now type the piece of code shown.
|- | 08.13 | We have declared a variable count and initialized it to zero
|- | 08.20 | In the while loop condition, we are checking if the variable count is greater than zero.
|-
| 08.30
|The condition is not true. So, the while loop code will not be executed even once.
|- | 08..36 |Then the do...while loop, we are first executing the code and then checking the condition.
|- | 08.45 | So, the code will be executed at least once.
|- | 08.50 | Then the condition whether the variable count is greater than zero, is checked.
|- |08.57 |The condition is not true. So the loop will exit.
|- |09.02 | Now, press ctrl+s to save the file.
|- |09.06 |Now, switch to terminal and type the following to check for any compilation or syntax errors
|-
| 09.13
|perl hyphen c loop dot pl and press Enter.
|- |09.19 | The following line will be displayed on the terminal loop dot pl syntax OK
|- | 09.27 | As there are no compilation or syntax errors, let us execute the Perl script.
|- | 09.32 |By Typing
|-
| 09.34
|perl loop dot pl and press Enter.
|- | 09.39 | The following output will be displayed on the terminal.
|-
| 09.44
|I am in do-while loop
|- | 09.47 | Here we can see, there is no output message displaying 'I am in while loop'
|-
| 09.52
|This message was what we printed inside the while loop.
|- | 10.00 | This implies that,
|-
| 10.02
|do-while loop executes at-least once before evaluating the condition
|-
| 10.08
|whereas while loop does not get executed even once when the condition specified is false.
|- | 10.16 | I hope the difference is clear to you now.
|-
| 10.19
|That's all there is to while and do-while loops.
|- | 10.23 | Let us summarize.
|- | 10.25 |In this tutorial, we have learnt -
|- | 10.27 |while loop and do-while loop in Perl
|- | 10.29 |using sample programs.
|- |10.32 |Here is assignment for you -
|- |10.34 |Print 'Hello Perl' till the count of variable reaches 10
|- |10.39 |by using while loop and do-while loops
|- | 10.42 | | Watch the video available at the following link
|-
|10.46
|It summaries the Spoken Tutorial project
|-
|10.49
|If you do not have good bandwidth, you can download and watch it
|- | 10.54 | The Spoken Tutorial Project Team
|-
|10.57
|Conducts workshops using spoken tutorials
|-
|11.00
|Gives certificates to those who pass an online test
|-
|11.04
|For more details, please write to contact at spoken hyphen tutorial dot org
|- | 11.12 | Spoken Tutorial Project is a part of the Talk to a Teacher project
|-
|11.17
|It is supported by the National Mission on Education through ICT, MHRD, Government of India.
|-
|11.25
|More information on this Mission is available spoken hypen tutorial dot org slash NMEICT hyphen Intro
|- | 11.37 | Hope you enjoyed this Perl tutorial.
|- |11.39 |This is Amol signing off.Thanks for joining