Difference between revisions of "PERL/C2/while-do-while-loops/English-timed"

From Script | Spoken-Tutorial
Jump to: navigation, search
 
(21 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 
{| border=1
 
{| border=1
!'''Visual Cue'''
+
|'''Time'''
! '''Narration'''
+
|'''Narration'''
 
+
 
|-
 
|-
| 00.01
+
| 00:01
|| Welcome to the spoken tutorial on '''while and do-while '''loops in '''Perl'''
+
|Welcome to the '''spoken tutorial''' on '''while and do-while loops''' in '''Perl'''.
 
+
 
|-
 
|-
| 00.07
+
| 00:06
|In this tutorial, we will learn about;'''while '''loop in '''Perl'''
+
| In this tutorial, we will learn about:
  
 
|-
 
|-
| 00.11
+
|00:09
|'''do-while '''loop in '''Perl'''
+
| '''while''' loop in '''Perl'''  
  
 
|-
 
|-
| 00.13
+
|00:11
| I am using '''Ubuntu Linux12.04''' operating system and '''Perl 5.14.2'''
+
| '''do-while''' loop in '''Perl '''.  I am using '''Ubuntu Linux 12.04''' operating system and '''Perl 5.14.2'''.
 
+
 
+
 
|-
 
|-
| 00.21
+
|00:20
|I will also be using the '''gedit '''Text Editor.
+
|I will also be using the '''gedit Text Editor'''.
 
+
 
+
 
|-
 
|-
| 00.25
+
|00:24
 
|You can use any text editor of your choice.
 
|You can use any text editor of your choice.
 
+
 
|-
 
|-
| 00.29
+
| 00:28
| You should have basic knowledge of '''Variables '''and''' Comments '''in''' Perl'''
+
| You should have basic knowledge of '''variables''' and '''comments''' in ''' Perl.'''
 
+
 
+
 
|-
 
|-
| 00.33
+
|00:33
|Knowledge of '''for '''and '''foreach '''loops in '''Perl '''will be an added advantage'''.'''
+
|Knowledge of '''for''' and '''foreach''' loops in '''Perl''' will be an added advantage.
 
+
  
 
|-
 
|-
| 00.39
+
|00:38
|Please go through the relevant spoken tutorials on the '''spoken tutorial '''website.
+
|Please go through the relevant spoken tutorials on the "Spoken Tutorial" website.  
 
+
 
|-
 
|-
| 00.44
+
| 00:43
|  '''while '''loop in '''Perl'''
+
| '''while''' loop in '''Perl''':
 
+
 
+
 
|-
 
|-
| 00.46
+
|00:45
|The '''while''' loop executes a block of code while a condition is true.
+
|The '''while''' loop executes a block of code while a condition is true.  
 
+
 
|-
 
|-
|  00.50
+
|00:50
|  The syntax of '''while''' loop is as follows -
+
|The syntax of '''while''' loop is as follows -  
 
+
 
+
 
|-
 
|-
| 00.53
+
|00:53
|'''while space '''open bracket''' condition''' close bracket Open curly bracket
+
|'''while''' space open bracket '''condition''' close bracket  
 
+
 
+
 
|-
 
|-
|01.00  
+
|00:58
|Piece of code to be executed while the condition is true. Close curly bracket
+
|open curly bracket
 
+
 +
|-
 +
|01:00
 +
|piece of code to be executed while the condition is true  
 +
 
|-
 
|-
|  01.07
+
|01:04
|  So, what happens if the condition is not satisfied? Then, the '''while''' loop will exit, without executing the code within, even once.
+
|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
+
|01:16
| Now let us look at an example of '''while''' loop.
+
|Now let us look at an example of '''while''' loop.  
  
 
|-
 
|-
|  01.20
+
|01:19
|  Open the '''Terminal''' and type '''gedit whileLoop dot pl space ampersand'' and press''' Enter'''
+
|Open the Terminal and type:
 +
 
|-
 
|-
|  
+
|01:22
|Point to the filename '''whileLoop'''.'''pl''' in the Titlebar of gedit.
+
|'''gedit whileLoop dot pl space ampersand'''
 
+
 
 
+
 
|-
 
|-
|  
+
|01:29
|Type the piece of code. '''#!/usr/bin/perl''' '''$i = 0;'''
+
|and press '''Enter'''.
 
+
 
|-
 
|-
|  
+
|01:31
|'''while ($i<=4) {''' '''print “Value of i: $i\n”;''' '''$i++;'' '''}'''
+
|This will open the '''whileLoop.pl''' file in '''gedit. '''
 
+
 
+
 
+
 
|-
 
|-
| 01.31
+
|01:34
|This will open the '''whileLoop'''.'''pl''' file in '''gedit'''.
+
| Now type the following code:
 
+
 
+
 
|-
 
|-
| 01.35
+
| 01:37
|Now Type the following code
+
| '''hash exclamation mark slash u s r slash bin slash perl'''
 
+
 
+
 
|-
 
|-
| 01.37
+
|01:45
|'''hash exclamation mark slash u s r slash bin slash perl''' Press '''Enter.'''
+
|press '''Enter'''.
 
+
 
+
 
|-
 
|-
| 01.47
+
|01:47
|'''dollar i''' is equal to zero '''semicolon'''
+
| 'dollar i' ($i) equals to zero semicolon
 
+
 
|-
 
|-
|01.52
+
| 01:52
|Press '''Enter'''
+
| press '''Enter '''.
 
+
 
+
 
|-
 
|-
| 01.54
+
|01:54
1. |While''' open bracket dollar i less than or equal to four close bracket space
+
|'''while''' open bracket '''dollar i less than or equal to four''' close bracket space  
 
+
 
+
 
|-
 
|-
| 02.04
+
|02:04
|Open curly bracket press enter and type
+
|open curly bracket press '''Enter''' and type:
 
+
 
+
 
|-
 
|-
| 02.08
+
|02:08
|'''print''' space double quote Value of i colon dollar i '''slash n double '''quote complete '''semicolon'''
+
|'''print''' space double quote '''Value of i''' colon ''' dollar i backslash n''' double quote complete semicolon
 
+
 
|-
 
|-
| 02.21
+
|02:20
|Press '''Enter'''
+
|press '''Enter'''.
 
+
 
+
 
|-
 
|-
| 02.23
+
|02:22
|'''dollar i plus plus semicolon''' press enter and
+
| '''dollar i plus plus semicolon'''
 
+
 
 
|-
 
|-
| 02.29
+
|02:27
|Close the curly bracket
+
| press '''Enter'''  and close the curly bracket.
 
+
 
|-
 
|-
|  02.31
+
|02:31
| Let me explain the '''while''' loop in detail.
+
|Let me explain the '''while''' loop in detail.  
 +
 
|-
 
|-
| 02.34
+
| 02:33
|We have initialized the variable''' i''' to 0.  
+
| We have initialized the variable 'i' to 0.  
  
 
|-
 
|-
| 02.39
+
|02:38
| Now we have specified the condition for '''while''' loop as '''$i '''less than or equal to 4.  
+
|Now, we have specified the condition for '''while''' loop as '''$i''' less than or equal to 4.  
 
+
 
|-
 
|-
| 02.47
+
|02:46
|If the '''condition''' is true, the code within the '''while''' loop will get executed.
+
| If the 'condition' is true, the code within the '''while''' loop will get executed.  
  
 
|-
 
|-
| 02.52
+
|02:52
|  This means, first time our '''while''' loop will print ''''Value of i: 0' '''on the '''terminal'''.
+
|This means, first time our '''while''' loop will print "Value of i: 0" on the terminal.  
 
+
 
|-
 
|-
| 03.01
+
|03:01
|  Then the '''$i++'''will increments the value of variable '''i '''by one.
+
|Then the '''$i++''' will increment the value of variable 'i' by one.  
  
 
|-
 
|-
|03.08
+
| 03:08
| Now again, the loop condition '''$i<=4''' will be evaluated.
+
|Now again, the loop condition ''' $i<=4''' will be evaluated.  
 
+
 
+
 
|-
 
|-
| 03.17
+
|03:16
|And the loop will exit once the value of''' i''' becomes 5.
+
|And the loop will exit once the value of 'i' becomes 5.  
 
+
 
|-
 
|-
|03.22
+
| 03:22
| In this case, '''while''' loop will get executed for''' i''' equal to 0, 1, 2, 3, 4.
+
| In this case, '''while''' loop will get executed for '''i''' equal to 0, 1, 2, 3 and 4.
 
+
 
|-
 
|-
|03.32
+
|   03:32
|Press '''ctrl+s''' to '''save''' the file.
+
| Press '''Ctrl+s''' to '''save''' the file.  
 
+
 
|-
 
|-
|03.35
+
|03:35
|Now, switch to the '''terminal'''.
+
|Now, switch to the terminal.  
 
+
 
|-
 
|-
| 03.38
+
| 03:37
|  Type the following to check for any compilation or syntax error -
+
| Type the following to check for any compilation or syntax error
 
+
 
+
 
|-
 
|-
| 03.43
+
|03:42
|'''perl hyphen c whileLoop dot pl''' and press''' Enter.'''
+
|'''perl hyphen c whileLoop dot pl'''
 
+
 
|-
 
|-
|  03.49
+
| 03:47
| The following line will be displayed on the terminal  '''whileLoop.pl syntax OK'''
+
| and press '''Enter.'''
 
+
 
|-
 
|-
| 03.56
+
| 03:49
| As there is  no compilation or syntax error, we will execute the '''Perl''' script by typing -
+
|The following line will be displayed on the terminal.
 
+
 
+
 
|-
 
|-
| 04.03
+
|03:52
|'''perl whileLoop dot pl''' and press '''Enter'''
+
|'''whileLoop.pl syntax OK '''
 
+
 
|-
 
|-
|   04.10
+
|03:56
| The following output will be displayed on the '''terminal'''.
+
|As there is no compilation or syntax error, we will execute the '''Perl''' script by typing: 
 
+
 
|-
 
|-
| 04.15
+
|04:02
|Now, let us look at '''do-while loop'''
+
|''' perl whileLoop dot pl'''
 
+
 
|-
 
|-
|  04.20
+
|04:06
| The '''do...while''' statement will always execute the piece of code at-least once.
+
|and press '''Enter'''.
 
+
 
+
 
|-
 
|-
| 04.26
+
|04:09
|It will then check the '''condition''' and repeat the loop while the '''condition''' is true.
+
|The following output will be displayed on the terminal.  
 
+
 
|-
 
|-
| 04.31
+
| 04:14
| The syntax for '''do-while '''loop is as follows - '''do '''space
+
| Now, let us look at '''do-while loop'''.
  
 
|-
 
|-
| 04.36
+
|   04:20
|open curly bracket
+
| The '''do..while''' statement will always execute the piece of code at-least once.
 
+
 
|-
 
|-
| 04.38
+
| 04:25
|Piece of code to be executed while the '''condition''' is true
+
| It will then check the condition and repeat the loop while the condition is true.
 
+
 
|-
 
|-
| 04.42
+
| 04:30
|close curly bracket then space
+
|The syntax for '''do-while''' loop is as follows:
  
 
|-
 
|-
| 04.45
+
| 04:34
|'''while '''space within brackets '''condition''' and then '''semicolon'''
+
| '''do''' space  
 
+
 
|-
 
|-
| 04.51
+
|04:36
| Open the '''Terminal''' and type;
+
|open curly bracket
 
+
  
 
|-
 
|-
| 04.55
+
|04:38
|'''gedit doWhileLoop dot pl space ampersand'''
+
|piece of code to be executed while the condition is '''true'''  
 
+
 
+
 
|-
 
|-
| 05.03
+
|04:42
|and then press''' Enter'''
+
|close curly bracket then space
 
+
 
|-
 
|-
| 05.05
+
|04:45
| This will open the '''doWhileLoop'''.'''pl''' file in '''gedit'''.
+
|'''while''' space within the brackets '''condition''' and then semicolon.  
 
+
 
|-
 
|-
|  05.09
+
|04:50
|Type the following piece of code -
+
|Open the Terminal and type:
 
+
  
 
|-
 
|-
| 05.12
+
|04:54
|'''hash exclamation mark slash u s r slash bin slash perl'''
+
| '''gedit doWhileLoop dot pl space ampersand'''
 
+
 
+
 
|-
 
|-
| 05.20
+
| 05:03
|Press '''Enter'''
+
| and then press '''Enter'''.
 
+
  
 
|-
 
|-
| 05.21
+
| 05:05
|'''dollar i equals to zero semicolon press enter'''
+
| This will open  '''doWhileLoop.pl''' file in '''gedit.'''  
 
+
 
|-
 
|-
| 05.28
+
|05:09
|'''do space'''
+
| Type the following piece of code: 
 
+
 
|-
 
|-
| 05.30
+
|05:11
| open curly bracket enter type
+
|'''hash exclamation mark slash u s r slash bin slash perl'''  Press '''Enter'''.
 
+
 
|-
 
|-
| 05.33
+
|05:21
|'''print space double''' quote''' Value of i colon space dollar i slash n''' double quote complete '''semicolon'''
+
|'''dollar i equals to zero''' semicolon press '''Enter'''.
  
 
|-
 
|-
| 05.47
+
|05:27
|Press '''Enter'''
+
|'''do''' space
 
+
 
+
 
|-
 
|-
| 05.49
+
|05:29
|'''dollar i plus plus semicolon''' '''press enter'''
+
|open curly bracket '''Enter''' type:
  
 
|-
 
|-
| 05.54
+
|05:33
|close curly bracket
+
|'''print''' space double quote '''Value of i colon <space> dollar i backslash n'''  double quote complete semicolon
 
+
 
|-
 
|-
| 05.56
+
|05:46
|space '''while space '''open bracket '''dollar i less than or equal to four '''
+
|press '''Enter '''.
 
+
 
|-
 
|-
| 06.06
+
|05:48
|close bracket '''semicolon'''
+
|'''dollar i plus plus''' semicolon
 
+
 
|-
 
|-
|06.11
+
| 05:52
 | Here is the detail explanation of a '''do-while''' loop.
+
| press '''Enter '''.
 
+
 
|-
 
|-
| 06.14
+
| 05:54
|We have initialized the variable''' i''' to 0.
+
| close curly bracket.  
 
+
 
|-
 
|-
|  06.19
+
|05:56
|  First time, the '''do-while''' loop will print the output as ''''Value of i colon 0'''' on the terminal
+
|space '''while''' space open bracket '''dollar i less than or equal to four'''
 
+
 
+
 
|-
 
|-
| 06.26
+
| 06:06
|without checking for '''condition'''
+
| close bracket semicolon.
  
 
|-
 
|-
|  06.29
+
| 06:10
|   '''$i++''' will increment the value of variable''' i '''by one each time loop gets executed.
+
| Here is the detail explanation of a '''do-while''' loop.  
 
+
 
|-
 
|-
| 06.37
+
| 06:13
| Second time, the condition''' $i''' less than or equal to 4 will be checked.  
+
| We have initialized the variable i to 0.  
 
+
  
 
|-
 
|-
| 06.44
+
| 06:18
|If the '''condition''' is true, the loop will get executed again.
+
|First time, the '''do-while''' loop will print the output as  'Value of i colon 0' on the terminal without checking for condition.
 
+
 
+
 
|-
 
|-
| 06.49
+
|06:28
|In our case, second time the output displayed on '''terminal''' will be ''''Value of I colon 1'.'''
+
|'''$i++''' will increment the value of variable 'i' by one, each time loop gets executed.  
 
+
 
|-
 
|-
|  06.58
+
| 06:36
|  The loop will get executed till the '''condition''' becomes false that is when variable''' i '''becomes 5.
+
| Second time, the condition '$i' less than or equal to 4 will be checked.  
 
+
 
|-
 
|-
|  07.07
+
| 06:43
| Press '''ctrl+s''' to '''save''' the file.
+
| If the condition is '''True''', the loop will be executed again.  
  
 
|-
 
|-
| 07.09
+
|06:48
|  Now, switch to '''terminal''' and type the following to check for any compilation or syntax error.
+
|In our case, second time the output displayed on terminal will be  'Value of i colon 1'.
 
+
 
+
 
|-
 
|-
| 07.16
+
| 06:57
|'''perl hyphen c doWhileLoop dot pl''' and press''' Enter.'''
+
| The loop will get executed till the condition becomes '''False''' that is when variable 'i' becomes 5.  
 
+
 
|-
 
|-
| 07.24
+
| 07:05
| The following line will be displayed on the '''terminal'''  
+
| 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'''
 +
 +
|-
 +
| 07:21
 +
|  and press '''Enter.'''
  
 
|-
 
|-
| 07.27
+
| 07:23
 +
| The following line will be displayed on the terminal:
 +
 +
|-
 +
|  07:26
 
|'''doWhileLoop.pl syntax OK'''
 
|'''doWhileLoop.pl syntax OK'''
  
 
|-
 
|-
| 07.31
+
|07:30
|  As there are no compilation or syntax errors, we will now execute the '''Perl''' script.
+
|As there is no compilation or syntax error, we will now execute the '''Perl''' script.  
 
+
 
+
 
|-
 
|-
| 07.36
+
|07:36
|Type,'''perl doWhileLoop dot pl''' and press '''Enter'''
+
|Type: '''perl doWhileLoop dot pl'''
 
+
 
|-
 
|-
|  07.44
+
|07:41
|  The following output will be displayed on the '''terminal'''.
+
|and press '''Enter'''.
 +
 +
|-
 +
| 07:43
 +
| 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.48 
+
|07:53
|Now, let us see the actual difference between '''while''' and '''do-while''' loops.
+
| On the Terminal, type: 
  
 
|-
 
|-
|  07.53
+
|07:55
| Open the '''Terminal''' and type -
+
| '''gedit loop dot pl space ampersand'''
 
+
 +
|-
 +
|08:01
 +
| and press '''Enter'''.
  
 
|-
 
|-
| 07.56 
+
|08:03
|'''gedit loop dot pl space ampersand''' and press''' Enter'''
+
| This will open '''loop dot pl''' file in '''gedit. '''
  
 
|-
 
|-
| 08.03
+
|08:07
| This will open '''loop dot pl''' file in '''gedit'''.
+
| Now type the piece of code shown.  
  
 +
|-
 +
|08:12
 +
| We have declared a variable '''count''' and initialized it to zero.
  
 
|-
 
|-
| 08.08
+
|08:19
|Now type the piece of code shown.
+
|In the '''while''' loop condition, we are checking if the variable '''count''' is greater than zero.  
  
 
|-
 
|-
| 08.13
+
|08:29
| We have declared a variable '''count''' and initialized it to zero
+
|The condition is not true. So, the '''while''' loop code will not be executed even once.
  
 
|-
 
|-
|  08.20
+
|08:36
| In the '''while''' loop '''condition,''' we are checking if the variable '''count''' is greater than zero.
+
|In the '''do...while''' loop, we are first executing the code and then checking the condition.  
 
+
  
 
|-
 
|-
| 08.30
+
|08:44
|The '''condition''' is not true. So, the '''while''' loop code will not be executed even once.
+
|So, the code will be executed at least once.  
  
 
|-
 
|-
| 08..36
+
|08:49
|Then the '''do...while''' loop''',''' we are first executing the code and then checking the condition.
+
|Then the condition whether the variable '''count''' is greater than zero, is checked.  
  
 
|-
 
|-
|  08.45
+
|08:57
| So, the code will be executed at least once.
+
|The condition is not '''True'''. So, the loop will exit.  
  
 
|-
 
|-
| 08.50
+
|09:02
| Then the '''condition''' whether the variable '''count''' is greater than zero, is checked.
+
|Now, press '''Ctrl+s''' to '''save''' the file.  
  
 
|-
 
|-
|08.57
+
|09:05
|The '''condition''' is not true. So the loop will exit.
+
|Now, switch to terminal and type the following to check for any compilation or syntax errors.  
  
 
|-
 
|-
|09.02
+
|09:12
| Now, press '''ctrl+s''' to save the file.
+
|'''perl hyphen c loop dot pl'''
  
 
|-
 
|-
|09.06
+
|09:16
|Now, switch to terminal and type the following to check for any compilation or syntax errors
+
|and press '''Enter.'''
 
+
  
 
|-
 
|-
| 09.13
+
|09:19
|'''perl hyphen c loop dot pl''' and press''' Enter.'''
+
|The following line will be displayed on the terminal.  
  
 
|-
 
|-
|09.19
+
|09:22
|  The following line will be displayed on the terminal  '''loop dot pl syntax OK'''
+
|'''loop dot pl syntax OK'''
  
 
|-
 
|-
| 09.27
+
|09:26
|  As there are no compilation or syntax errors, let us execute the '''Perl''' script.
+
|As there is no compilation or syntax error, let us execute the '''Perl''' script  
  
 
|-
 
|-
| 09.32
+
|09:31
|By Typing
+
|by typing '''perl loop dot pl'''
 
+
  
 
|-
 
|-
| 09.34
+
|09:36
|'''perl loop dot pl''' and press '''Enter.'''
+
|and press '''Enter.'''
  
 
|-
 
|-
|  09.39
+
|09:38
|  The following output will be displayed on the terminal.
+
|The following output will be displayed on the terminal.  
 
+
  
 
|-
 
|-
| 09.44
+
|09:43
 
|'''I am in do-while loop'''
 
|'''I am in do-while loop'''
  
 
|-
 
|-
|  09.47
+
|09:46
| Here we can see, there is no output message displaying ''''I am in while loop''''  
+
|Here we can see, there is no output message displaying ''' 'I am in while loop' '''.
 
+
  
 
|-
 
|-
| 09.52
+
|09:52
|This message was what we printed inside the '''while''' loop.
+
|This message was what we printed inside the ''''while'''' loop.  
  
 
|-
 
|-
| 10.00
+
|09:59
|  This implies that,
+
|This implies that  
 
+
 
+
 
|-
 
|-
| 10.02
+
|10:01
|'''do-while''' loop executes at-least once before evaluating the '''condition'''
+
|'''do-while''' loop executes at-least once before evaluating the condition
 
+
  
 
|-
 
|-
| 10.08
+
|10:07
|whereas '''while''' loop does not get executed even once when the '''condition''' specified is false.
+
|whereas '''while''' loop does not get executed even once when the condition specified is '''False'''.  
  
 
|-
 
|-
| 10.16
+
|10:15
| I hope the difference is clear to you now.  
+
|I hope the difference is clear to you now.  
 
+
  
 
|-
 
|-
| 10.19
+
|10:18
|That's all there is to '''while''' and '''do-while''' loops.
+
|That's all there is to '''while''' and '''do-while''' loops.  
  
 
|-
 
|-
|  10.23
+
|10:22
|  Let us summarize.
+
|Let us summarize.  
  
 
|-
 
|-
| 10.25
+
|10:24
|In this tutorial, we have learnt -
+
|In this tutorial, we have learnt
  
 
|-
 
|-
| 10.27
+
|10:26
|'''while''' loop and '''do-while''' loop in '''Perl'''
+
|'''while''' loop and '''do-while''' loop in '''Perl'''  
  
 
|-
 
|-
| 10.29
+
|10:29
|using sample programs.
+
|using sample programs.  
  
 
|-
 
|-
|10.32
+
|10:31
|Here is assignment for you -
+
|Here is an assignment for you -  
  
 
|-
 
|-
|10.34
+
|10:33
|Print ''''Hello Perl'''' till the count of variable reaches 10
+
|Print ''' 'Hello Perl' '''
  
 
|-
 
|-
|10.39
+
|10:35
|by using '''while''' loop and '''do-while''' loops
+
|till the count of variable reaches 10
  
 
|-
 
|-
| 10.42
+
|10:38
| | Watch the video available at the following link
+
|by using '''while''' loop and '''do-while''' loops.
 
+
  
 
|-
 
|-
|10.46
+
|10:41
|It summaries the Spoken Tutorial project
+
|Watch the video available at the following link.
 
+
  
 
|-
 
|-
|10.49
+
|10:45
|If you do not have good bandwidth, you can download and watch it
+
|It summarizes the Spoken Tutorial project.
 
+
 
|-
 
|-
| 10.54
+
|10:49
|   The Spoken Tutorial Project Team
+
|If you do not have good bandwidth, you can download and watch it.
 
+
 
+
 
|-
 
|-
|10.57
+
| 10:53
|Conducts workshops using spoken tutorials
+
|The Spoken Tutorial Project Team:
 
+
  
 
|-
 
|-
|11.00
+
| 10:56
|Gives certificates to those who pass an online  test
+
| Conducts workshops using spoken tutorials.
 
+
  
 
|-
 
|-
|11.04
+
|11:00
|For more details, please write to contact at spoken hyphen tutorial dot org
+
| Gives certificates to those who pass an online test.
 
+
    
|-
+
| 11.12
+
|   Spoken Tutorial Project is a part of the Talk to a  Teacher project
+
 
+
 
+
 
|-
 
|-
|11.17
+
|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.
 
|It is supported by the National Mission on Education through ICT, MHRD, Government of India.
 
+
 
+
 
|-
 
|-
|11.25
+
|11:24
|More information on this Mission is available spoken hypen tutorial dot org slash NMEICT hyphen Intro
+
|More information on this mission is available at spoken hyphen tutorial dot org slash NMEICT hyphen Intro.
 
+
 
|-
 
|-
| 11.37
+
| 11:36
| Hope you enjoyed this '''Perl''' tutorial.  
+
| Hope you enjoyed this '''Perl''' tutorial.  
 
+
 +
|-
 +
|11:38
 +
|This is Amol Brahmankar, signing off.
 +
 
|-
 
|-
|11.39
+
|11:40
|This is Amol  signing off.Thanks for joining
+
|Thanks for joining.
 +
 +
|}

Latest revision as of 15:12, 10 March 2017

Time Narration
00:01 Welcome to the spoken tutorial on while and do-while loops in Perl.
00:06 In this tutorial, we will learn about:
00:09 while loop in Perl
00:11 do-while loop in Perl . I am using Ubuntu Linux 12.04 operating system and Perl 5.14.2.
00:20 I will also be using the gedit Text Editor.
00:24 You can use any text editor of your choice.
00:28 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:38 Please go through the relevant spoken tutorials on the "Spoken Tutorial" website.
00:43 while loop in Perl:
00:45 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
00:58 open curly bracket
01:00 piece of code to be executed while the condition is true
01:04 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:16 Now let us look at an example of while loop.
01:19 Open the Terminal and type:
01:22 gedit whileLoop dot pl space ampersand
01:29 and press Enter.
01:31 This will open the whileLoop.pl file in gedit.
01:34 Now type the following code:
01:37 hash exclamation mark slash u s r slash bin slash perl
01:45 press Enter.
01:47 'dollar i' ($i) equals to zero semicolon
01:52 press Enter .
01:54 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 backslash n double quote complete semicolon
02:20 press Enter.
02:22 dollar i plus plus semicolon
02:27 press Enter and close the curly bracket.
02:31 Let me explain the while loop in detail.
02:33 We have initialized the variable 'i' to 0.
02:38 Now, we have specified the condition for while loop as $i less than or equal to 4.
02:46 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 increment the value of variable 'i' by one.
03:08 Now again, the loop condition $i<=4 will be evaluated.
03:16 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 and 4.
03:32 Press Ctrl+s to save the file.
03:35 Now, switch to the terminal.
03:37 Type the following to check for any compilation or syntax error:
03:42 perl hyphen c whileLoop dot pl
03:47 and press Enter.
03:49 The following line will be displayed on the terminal.
03:52 whileLoop.pl syntax OK
03:56 As there is no compilation or syntax error, we will execute the Perl script by typing:
04:02 perl whileLoop dot pl
04:06 and press Enter.
04:09 The following output will be displayed on the terminal.
04:14 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:25 It will then check the condition and repeat the loop while the condition is true.
04:30 The syntax for do-while loop is as follows:
04:34 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 the brackets condition and then semicolon.
04:50 Open the Terminal and type:
04:54 gedit doWhileLoop dot pl space ampersand
05:03 and then press Enter.
05:05 This will open doWhileLoop.pl file in gedit.
05:09 Type the following piece of code:
05:11 hash exclamation mark slash u s r slash bin slash perl Press Enter.
05:21 dollar i equals to zero semicolon press Enter.
05:27 do space
05:29 open curly bracket Enter type:
05:33 print space double quote Value of i colon <space> dollar i backslash n double quote complete semicolon
05:46 press Enter .
05:48 dollar i plus plus semicolon
05:52 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:10 Here is the detail explanation of a do-while loop.
06:13 We have initialized the variable i to 0.
06:18 First time, the do-while loop will print the output as 'Value of i colon 0' on the terminal without checking for condition.
06:28 $i++ will increment the value of variable 'i' by one, each time loop gets executed.
06:36 Second time, the condition '$i' less than or equal to 4 will be checked.
06:43 If the condition is True, the loop will be executed again.
06:48 In our case, second time the output displayed on terminal will be 'Value of i colon 1'.
06:57 The loop will get executed till the condition becomes False that is when variable 'i' becomes 5.
07:05 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
07:21 and press Enter.
07:23 The following line will be displayed on the terminal:
07:26 doWhileLoop.pl syntax OK
07:30 As there is no compilation or syntax error, we will now execute the Perl script.
07:36 Type: perl doWhileLoop dot pl
07:41 and press Enter.
07:43 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 On the Terminal, type:
07:55 gedit loop dot pl space ampersand
08:01 and press Enter.
08:03 This will open loop dot pl file in gedit.
08:07 Now type the piece of code shown.
08:12 We have declared a variable count and initialized it to zero.
08:19 In the while loop condition, we are checking if the variable count is greater than zero.
08:29 The condition is not true. So, the while loop code will not be executed even once.
08:36 In the do...while loop, we are first executing the code and then checking the condition.
08:44 So, the code will be executed at least once.
08:49 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:05 Now, switch to terminal and type the following to check for any compilation or syntax errors.
09:12 perl hyphen c loop dot pl
09:16 and press Enter.
09:19 The following line will be displayed on the terminal.
09:22 loop dot pl syntax OK
09:26 As there is no compilation or syntax error, let us execute the Perl script
09:31 by typing perl loop dot pl
09:36 and press Enter.
09:38 The following output will be displayed on the terminal.
09:43 I am in do-while loop
09:46 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.
09:59 This implies that
10:01 do-while loop executes at-least once before evaluating the condition
10:07 whereas while loop does not get executed even once when the condition specified is False.
10:15 I hope the difference is clear to you now.
10:18 That's all there is to while and do-while loops.
10:22 Let us summarize.
10:24 In this tutorial, we have learnt:
10:26 while loop and do-while loop in Perl
10:29 using sample programs.
10:31 Here is an assignment for you -
10:33 Print 'Hello Perl'
10:35 till the count of variable reaches 10
10:38 by using while loop and do-while loops.
10:41 Watch the video available at the following link.
10:45 It summarizes the Spoken Tutorial project.
10:49 If you do not have good bandwidth, you can download and watch it.
10:53 The Spoken Tutorial Project Team:
10:56 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:24 More information on this mission is available at spoken hyphen tutorial dot org slash NMEICT hyphen Intro.
11:36 Hope you enjoyed this Perl tutorial.
11:38 This is Amol Brahmankar, signing off.
11:40 Thanks for joining.

Contributors and Content Editors

Gaurav, PoojaMoolya, Sandhya.np14, Sneha