Difference between revisions of "BASH/C2/Conditional-Loops/English-timed"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with "'''Title of script:''' '''Basic If loop in BASH ''' '''Author:''' Ashwini Patil '''Keywords: video tutorial, for loop, while loop''' {| border=1 !Time !Narration |- | 00:0...")
 
 
(5 intermediate revisions by 2 users not shown)
Line 1: Line 1:
'''Title of script:''' '''Basic If loop in BASH '''
 
'''Author:''' Ashwini Patil
 
'''Keywords: video tutorial, for loop, while loop'''
 
 
 
{| border=1  
 
{| border=1  
!Time  
+
|'''Time'''
!Narration  
+
|'''Narration'''
 
|-
 
|-
 
| 00:01
 
| 00:01
| Dear friends, welcome to the spoken tutorial on '''loops''' in '''BASH'''.
+
| Dear friends, welcome to the '''spoken tutorial''' on '''loops''' in '''BASH'''.
  
 
|-
 
|-
 
| 00:07   
 
| 00:07   
|  In this tutorial, we will learn
+
|  In this tutorial, we will learn:
  
 
|-
 
|-
 
| 00:09   
 
| 00:09   
| * '''for loop'''  
+
|'''for loop'''  
  
 
|-
 
|-
 
| 00:11   
 
| 00:11   
| * '''while loop'''<br/> with a few examples
+
|'''while loop'''<br/> with a few examples.
 
|-
 
|-
 
| 00:15
 
| 00:15
| To record this tutorial I am using,
+
| To record this tutorial, I am using:
  
 
|-
 
|-
 
| 00:18
 
| 00:18
| * '''Ubuntu Linux''' 12.04 '''Operating System '''
+
|'''Ubuntu Linux''' 12.04 '''Operating System '''
  
 
|-
 
|-
 
| 00:22
 
| 00:22
| * '''GNU BASH''' version 4.1.10
+
|'''GNU BASH''' version 4.1.10
  
 
|-
 
|-
 
| 00:26
 
| 00:26
| Please note,'''GNU bash''' version '''4''' or above is recommended to practice this tutorial.
+
| Please note, '''GNU bash''' version 4 or above is recommended to practice this tutorial.
  
 
|-
 
|-
| 00:34  
+
| 00:34  
|  Let us start with an introduction to ''' loops'''
+
|  Let us start with an introduction to ''' loops'''.
  
 
|-
 
|-
| 00:37  
+
| 00:37  
| '''Loops''' are used to execute a group of statements repeatedly.
+
| '''Loops''' are used to '''execute''' a group of '''statements''' repeatedly.
  
 
|-
 
|-
| 00:43
+
| 00:43
 
|  Let us see the syntax.
 
|  Let us see the syntax.
  
 
|-
 
|-
| 00:45
+
| 00:45
 
| '''for expression 1, 2, 3'''
 
| '''for expression 1, 2, 3'''
  
 
|-
 
|-
| 00:49
+
| 00:49
 
| '''statement 1, 2, 3'''
 
| '''statement 1, 2, 3'''
  
 
|-
 
|-
| 00:51
+
| 00:51
| And this is the end of '''for loop'''.
+
| and this is the end of '''for loop'''.
  
 
|-
 
|-
| 00:55
+
| 00:55
 
|  An alternate syntax of '''for loop '''is:
 
|  An alternate syntax of '''for loop '''is:
  
 
|-
 
|-
| 00:58
+
| 00:58
 
| '''for variable in sequence/range'''
 
| '''for variable in sequence/range'''
  
 
|-
 
|-
| 01:03
+
| 01:03
 
| '''statement 1, 2, 3'''
 
| '''statement 1, 2, 3'''
  
 
|-
 
|-
| 01:06
+
| 01:06
| And end of '''for loop'''.
+
| and end of '''for loop'''.
  
 
|-
 
|-
| 01:09
+
| 01:09
|   Let us see an example of '''for loop''' using the first syntax.
+
| Let us see an example of '''for loop''' using the first syntax.
  
 
|-
 
|-
| 01:14
+
| 01:14
 
| In this program, we will calculate the sum of first '''n''' numbers.
 
| In this program, we will calculate the sum of first '''n''' numbers.
 
|-
 
|-
| 01: 20  
+
| 01:20  
|  Note that our filename is '''for.sh'''
+
|  Note that our filename is '''for.sh'''.
  
 
|-
 
|-
| 01: 25  
+
| 01:25  
 
|  This is our '''shebang line'''.
 
|  This is our '''shebang line'''.
  
 
|-
 
|-
| 01: 28
+
| 01:28
|  The variable '''number''' will store the value given by user.  
+
|  The variable '''number''' will store the '''value''' given by user.  
  
 
|-
 
|-
|  01: 34
+
|  01:34
 
| The value is an integer here.
 
| The value is an integer here.
  
 
|-
 
|-
|  01: 37
+
|  01:37
| Now, we initialize the variable '''sum''' as zero.
+
| Now, we initialize the '''variable''' '''sum''' as zero.
  
 
|-
 
|-
|  01: 42  
+
|  01:42  
 
|  This is where we begin the '''for loop.'''
 
|  This is where we begin the '''for loop.'''
  
 
|-
 
|-
| 01: 45  
+
| 01:45  
| First, we initialize '''i''' to '''1'''.
+
| First, we initialize 'i' to 1.
  
 
|-
 
|-
| 01: 48  
+
| 01:48  
| Then we check whether '''i''' is less than or equal to '''number'''.
+
| Then we check whether 'i' is less than or equal to '''number'''.
  
 
|-
 
|-
|   01: 54
+
| 01:54
|  Now here, we calculate the '''sum''' as '''sum plus I'''.
+
|  Now here, we calculate the '''sum''' as '''sum plus i'''
  
 
|-
 
|-
|  02: 00
+
|  02:00
 
| and then we print it.
 
| and then we print it.
  
 
|-
 
|-
| 02: 03   
+
| 02:03   
|  After this, we increment the value of '''i''' by '''1'''.
+
|  After this, we increment the '''value''' of 'i' by 1.
  
 
|-
 
|-
| 02: 08
+
| 02:08
|  And then we check the condition till this condition is '''false'''.
+
|  And then we check the '''condition''' till this '''condition''' is '''False'''.
 
|-
 
|-
|  02: 14
+
|  02:14
 
|  On exiting the '''for loop,''' this message is printed.
 
|  On exiting the '''for loop,''' this message is printed.
  
 
|-
 
|-
|  02: 19
+
|  02:19
|  Let us execute the program and observe what happens.
+
|  Let us '''execute''' the program and observe what happens.
  
 
|-
 
|-
| 02: 24   
+
| 02:24   
|  On the '''terminal''' type - '''chmod +x for.sh'''
+
|  On the '''terminal''', type - '''chmod +x for.sh'''
  
 
|-
 
|-
| 02: 31   
+
| 02:31   
 
| Then type: '''./for.sh'''
 
| Then type: '''./for.sh'''
  
 
|-
 
|-
|  02: 36   
+
|  02:36   
|  I will enter '''5''' as the input number.
+
|  I will enter '''5''' as the '''input''' number.
  
 
|-
 
|-
|  02: 40   
+
|  02:40   
| The '''sum''' that is calculated for each value of '''I''' is displayed.
+
| The '''sum''' that is calculated for each value of 'i' is displayed.
  
 
|-
 
|-
|  02: 46   
+
|  02:46   
| After that, the last line of the output is displayed:
+
| After that, the last line of the '''output''' is displayed:
  
 
|-
 
|-
|  02: 50
+
|  02:50
| '''Sum of first n numbers is 15'''
+
| '''"Sum of first n numbers is 15"'''.
  
 
|-
 
|-
|  02: 54
+
|  02:54
| Now let us see the flow of the program.
+
| Now, let us see the flow of the program.
  
 
|-
 
|-
|  02: 57
+
|  02:57
| Let me resize the windows.
+
| Let me resize the '''windows'''.
  
 
|-
 
|-
|  03: 00
+
|  03:00
|  First we have value of '''i''' as '''1'''.
+
|  First, we have value of 'i' as 1.
  
 
|-
 
|-
|  03: 04  
+
|  03:04  
|  Then we check whether ''' 1 ''' is less than or equal to ''' 5.'''
+
|  Then we check whether '''1''' is less than or equal to ''' 5.'''
  
 
|-
 
|-
|  03: 10
+
|  03:10
| Since the condition is '''true,''' we calculate '''sum''' as ''' 0 + 1.'''
+
| Since the condition is '''True,''' we calculate the '''sum''' as '''0 + 1.'''
  
 
|-
 
|-
|  03: 16
+
|  03:16
| Now we have '''sum '''as '''1.'''
+
| Now we have the '''sum '''as '''1.'''
  
 
|-
 
|-
|  03: 20
+
|  03:20
 
| Then we print '''sum ''' i.e '''1.'''
 
| Then we print '''sum ''' i.e '''1.'''
  
 
|-
 
|-
|   03: 24  
+
| 03:24  
|  Next, '''I''' is incremented by '''1''' and the new value of ''' i ''' is ''' 2.'''
+
|  Next, 'i' is incremented by '''1''' and the new value of 'i' is '''2.'''
  
 
|-
 
|-
|   03: 31  
+
| 03:31  
| Then we check whether ''' 2 ''' is less than or equal to ''' 5.'''
+
| Then we check whether ''' 2 ''' is less than or equal to ''' 5.'''
  
 
|-
 
|-
|  03: 36  
+
|  03:36  
| The condition is '''true''' and now sum will be ''' 1 + 2 ''' i.e ''' 3.'''
+
| The '''condition''' is '''True''' and now '''sum''' will be ''' 1 + 2 ''' i.e ''' 3.'''
  
 
|-
 
|-
|   03: 44
+
| 03:44
|    '''i ''' is then incremented by '''1''' and then the new value of ''' i''' is ''' 3.'''
+
|    'i' is then incremented by '''1''' and then the new value of 'i' is '''3.'''
  
 
|-
 
|-
| 03: 51
+
| 03:51
 
| And we get '''sum''' as ''' 6.'''
 
| And we get '''sum''' as ''' 6.'''
  
 
|-
 
|-
| 03: 55  
+
| 03:55  
|  The script will continue to add the next value of ''' I ''' to the previous value in ''' sum.'''
+
|  The '''script''' will continue to add the next value of 'i' to the previous value in ''' sum.'''
  
 
|-
 
|-
| 04: 02  
+
| 04:02  
| This will continue till '''<nowiki>i<=5</nowiki> is false.'''
+
| This will continue till '''<nowiki>i<=5</nowiki>''' is '''False.'''
  
 
|-
 
|-
| 04: 09  
+
| 04:09  
 
|  On exiting the '''for loop,''' the final message is printed.
 
|  On exiting the '''for loop,''' the final message is printed.
  
 
|-
 
|-
| 04: 14   
+
| 04:14   
 
|  Let us see another example of '''for loop'''using the second syntax.
 
|  Let us see another example of '''for loop'''using the second syntax.
  
 
|-
 
|-
| 04: 20   
+
| 04:20   
| I have written the code in this file and named it as '''for-loop.sh'''
+
| I have written the '''code''' in this file and named it as '''for-loop.sh'''.
  
 
|-
 
|-
| 04: 27   
+
| 04:27   
 
| This simple program will list the files in a directory.
 
| This simple program will list the files in a directory.
  
 
|-
 
|-
| 04: 32  
+
| 04:32  
| This is the '''shebang line.'''
+
| This is the '''shebang line.'''
  
 
|-
 
|-
| 04: 35  
+
| 04:35  
|  Then we have '''for loop'''.
+
|  Then we have '''for''' loop.
  
 
|-
 
|-
| 04: 37  
+
| 04:37  
| '''ls''' command lists the directory content.
+
| ''' 'ls' ''' '''command''' lists the directory content.
  
 
|-
 
|-
| 04: 41  
+
| 04:41  
 
| '''-1 (hyphen one)''' lists one file per line.
 
| '''-1 (hyphen one)''' lists one file per line.
  
 
|-
 
|-
| 04: 46  
+
| 04:46  
| This will list all the files present in your home directory.
+
| This will list all the files present in your '''home directory'''.
  
 
|-
 
|-
|  04: 51  
+
|  04:51  
 
| This is the end of '''for loop.'''
 
| This is the end of '''for loop.'''
  
 
|-
 
|-
| 04: 53
+
| 04:53
|  Let us execute the script on the '''terminal''' by typing -  
+
|  Let us '''execute''' the '''script''' on the '''terminal''' by typing -  
  
 
|-
 
|-
| 04: 58
+
| 04:58
 
| '''chmod +x for-loop.sh'''
 
| '''chmod +x for-loop.sh'''
  
 
|-
 
|-
| 05: 04
+
| 05:04
| '''./for-loop.sh'''
+
| Type: '''./for-loop.sh'''
  
 
|-
 
|-
| 05: 09
+
| 05:09
 
|  This will display all the files present in the '''Home''' directory.
 
|  This will display all the files present in the '''Home''' directory.
  
 
|-
 
|-
| 05: 14   
+
| 05:14   
|  Now, we will learn about ''' while loop.'''
+
|  Now we will learn about ''' while loop.'''
  
 
|-
 
|-
| 05: 18   
+
| 05:18   
 
| Let us understand the syntax first.
 
| Let us understand the syntax first.
  
 
|-
 
|-
| 05: 21  
+
| 05:21  
 
|  '''while condition'''
 
|  '''while condition'''
 
'''statement 1, 2, 3'''
 
'''statement 1, 2, 3'''
End of '''while loop'''.
+
end of '''while loop'''.
  
 
|-
 
|-
|   05: 27
+
| 05:27
| This means that the '''while loop''' will execute as long as the condition is '''true.'''
+
| This means that the '''while''' loop will execute as long as the '''condition''' is '''True.'''
  
 
|-
 
|-
| 05: 34
+
|05:34
|   Let us see an example of '''while loop.'''
+
|Let us see an example of '''while loop.'''
  
 
|-
 
|-
|  05: 37
+
|  05:37
| Here I have named it as '''while.sh'''
+
| Here, I have named it as '''while.sh'''.
  
 
|-
 
|-
|  05: 42
+
|  05:42
 
| In this program, we will calculate the sum of even numbers within a given range.
 
| In this program, we will calculate the sum of even numbers within a given range.
  
 
|-
 
|-
|   05: 49
+
|05:49
|   Let us go through the code.
+
| Let us go through the '''code'''.
  
 
|-
 
|-
| 05: 52
+
|05:52
 
| Here, we accept a number from the user and store it in variable '''number'''.
 
| Here, we accept a number from the user and store it in variable '''number'''.
  
 
|-
 
|-
| 05: 59
+
|05:59
|  Next, we declare variables '''i''' and '''sum''' and initialize them to ''' 0 (zero).'''
+
|  Next, we declare variables 'i' and '''sum''' and initialize them to 0 (zero).
  
 
|-
 
|-
| 06: 06
+
|06:06
|   Now this is the '''while condition.'''
+
|Now, this is the '''while''' '''condition'''.
  
 
|-
 
|-
| 06: 08
+
| 06:08
| Here we check whether '''i''' is '''less than or equal''' to value of '''number''' given by the user.
+
| Here, we check whether 'i' is '''less than or equal''' to value of '''number''' given by the user.
  
 
|-
 
|-
 
| 06:17   
 
| 06:17   
|  Then we calculate the '''sum''' by adding value of '''i''' to value of '''sum'''.
+
|  Then we calculate the '''sum''' by adding value of 'i' to value of '''sum'''.
  
 
|-
 
|-
 
|  06:24  
 
|  06:24  
|  Next, we increment the value of '''i''' with '''2'''.
+
|  Next, we increment the value of 'i' with '''2'''.
  
 
|-
 
|-
|  06: 28  
+
|  06:28  
 
| This will ensure that we only add the even numbers.
 
| This will ensure that we only add the even numbers.
  
 
|-
 
|-
|  06: 33  
+
|  06:33  
| And the while loop is repeated till the value of '''i''' exceeds the value of '''number.'''
+
| And the '''while loop''' is repeated till the value of 'i' exceeds the value of '''number.'''
 
|-
 
|-
|  06: 40  
+
|  06:40  
 
|  When we exit the '''while loop, '''we print the sum of all the even numbers within the given range.
 
|  When we exit the '''while loop, '''we print the sum of all the even numbers within the given range.
  
 
|-
 
|-
|   06:47  
+
| 06:47  
| Let us execute the program.
+
| Let us '''execute''' the program.
  
 
|-
 
|-
| 06:50  
+
| 06:50  
 
|  Type on the '''terminal''':
 
|  Type on the '''terminal''':
  
 
|-
 
|-
| 06:52  
+
| 06:52  
 
| '''chmod +x while.sh'''
 
| '''chmod +x while.sh'''
  
Line 370: Line 366:
 
|-
 
|-
 
|  07:06  
 
|  07:06  
| '''Sum of even numbers within the given range is 56.'''
+
| '''"Sum of even numbers within the given range is 56".'''
  
 
|-
 
|-
| 07:11  
+
| 07:11  
|  Let me resize the window and explain the output.
+
|  Let me resize the '''window''' and explain the '''output'''.
  
 
|-
 
|-
 
| 07:14  
 
| 07:14  
|  First we check whether '''i''' which is '''0,''' is less than or equal to the '''number''', which is '''15'''.
+
|  First we check whether 'i' which is 0, is less than or equal to the '''number''' which is '''15'''.
  
 
|-
 
|-
 
|  07:24  
 
|  07:24  
|  The condition is '''true,''' hence '''sum''' will be '''0+0''' I.e '''0.'''
+
|  The '''condition''' is '''True.''' Hence '''sum''' will be '0+0' i.e 0.
  
 
|-
 
|-
 
|  07:31
 
|  07:31
| Now '''i''' will be incremented by '''2''' and the '''new''' value of ''' i ''' is ''' 2.'''
+
| Now 'i' will be incremented by '''2''' and the new value of 'i' is ''' 2.'''
  
 
|-
 
|-
|   07:37
+
| 07:37
 
|  Then we check if '''2''' is less than or equal to '''15'''.
 
|  Then we check if '''2''' is less than or equal to '''15'''.
  
 
|-
 
|-
 
|  07:43  
 
|  07:43  
|  The condition again is '''true;''' so we add '''0+2.'''
+
|  The condition again is '''True;''' so we add '''0+2.'''
  
 
|-
 
|-
Line 402: Line 398:
 
|-
 
|-
 
|  07:52
 
|  07:52
|  Again the value of '''i''' will be incremented by '''2'''.
+
|  Again the value of 'i' will be incremented by '''2'''.
  
 
|-
 
|-
 
|  07:56
 
|  07:56
| So now value of '''i''' will be '''2+2''' I.e '''4.'''
+
| So, now value of 'i' will be '''2+2''' i.e '''4.'''
  
 
|-
 
|-
 
|  08:03
 
|  08:03
|  And the next value of ''' sum ''' will be '''4+2''' I.e '''6.'''
+
|  And the next value of ''' sum ''' will be '''4+2''' i.e '''6.'''
  
 
|-
 
|-
 
|  08:09
 
|  08:09
|  In the same way, the script will continue to add '''2''' to the previous value of '''i, '''till it exceeds '''15'''.
+
|  In the same way, the '''script''' will continue to add '''2''' to the previous value of 'i', till it exceeds '''15'''.
  
 
|-
 
|-
Line 426: Line 422:
 
|-
 
|-
 
| 08:27   
 
| 08:27   
| Let us summarize
+
| Let us summarize. In this tutorial, we learned two different syntax of '''for loop ''' and we also learned about the '''while loop.'''
 
+
|-
+
| 08:28 
+
| In this tutorial we learnt two different syntax of '''for loop ''' and we also learnt about the '''while loop.'''
+
  
 
|-
 
|-
 
| 08:37   
 
| 08:37   
|   As an assignment -
+
| As an assignment -find the sum of the first "n" prime numbers.
 
+
|-
+
| 08:38 
+
| Find the sum of the first "n" prime numbers.
+
  
 
|-
 
|-
 
|  08:43  
 
|  08:43  
|  Watch the video available at the link shown below
+
|  Watch the video available at the link shown below.
  
 
|-
 
|-
 
|  08:46  
 
|  08:46  
| It summarises the Spoken Tutorial project  
+
| It summarizes the Spoken-Tutorial project.
  
 
|-
 
|-
 
|  08:50
 
|  08:50
| If you do not have good bandwidth, you can download and watch it
+
| If you do not have good bandwidth, you can download and watch it.
  
 
|-
 
|-
 
| 08:54   
 
| 08:54   
|  The Spoken Tutorial Project Team
+
|  The Spoken Tutorial Project team:
  
 
|-
 
|-
 
| 08:56   
 
| 08:56   
| Conducts workshops using spoken tutorials  
+
|Conducts workshops using spoken tutorials.
  
 
|-
 
|-
 
| 09:00   
 
| 09:00   
| Gives certificates to those who pass an online test  
+
|Gives certificates to those who pass an online test.
  
 
|-
 
|-
Line 469: Line 457:
  
 
|-
 
|-
| 09:11  
+
|09:11  
| Spoken Tutorial Project is a part of the Talk to a Teacher project
+
|'''Spoken Tutorial''' Project is a part of the '''Talk to a Teacher''' project.
  
 
|-
 
|-
| 09:14  
+
| 09:14  
| 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.
  
 
|-
 
|-
 
|  09:22  
 
|  09:22  
| More information on this Mission is available at the link shown below.
+
| More information on this mission is available at the link shown below.
  
 
|-
 
|-
Line 486: Line 474:
 
|-
 
|-
 
|  09:34  
 
|  09:34  
|  This is Ashwini Patil from IIT Bombay signning off.
+
|  This is Ashwini Patil from IIT Bombay, signing off.
  
 
|-
 
|-

Latest revision as of 16:24, 23 March 2017

Time Narration
00:01 Dear friends, welcome to the spoken tutorial on loops in BASH.
00:07 In this tutorial, we will learn:
00:09 for loop
00:11 while loop
with a few examples.
00:15 To record this tutorial, I am using:
00:18 Ubuntu Linux 12.04 Operating System
00:22 GNU BASH version 4.1.10
00:26 Please note, GNU bash version 4 or above is recommended to practice this tutorial.
00:34 Let us start with an introduction to loops.
00:37 Loops are used to execute a group of statements repeatedly.
00:43 Let us see the syntax.
00:45 for expression 1, 2, 3
00:49 statement 1, 2, 3
00:51 and this is the end of for loop.
00:55 An alternate syntax of for loop is:
00:58 for variable in sequence/range
01:03 statement 1, 2, 3
01:06 and end of for loop.
01:09 Let us see an example of for loop using the first syntax.
01:14 In this program, we will calculate the sum of first n numbers.
01:20 Note that our filename is for.sh.
01:25 This is our shebang line.
01:28 The variable number will store the value given by user.
01:34 The value is an integer here.
01:37 Now, we initialize the variable sum as zero.
01:42 This is where we begin the for loop.
01:45 First, we initialize 'i' to 1.
01:48 Then we check whether 'i' is less than or equal to number.
01:54 Now here, we calculate the sum as sum plus i
02:00 and then we print it.
02:03 After this, we increment the value of 'i' by 1.
02:08 And then we check the condition till this condition is False.
02:14 On exiting the for loop, this message is printed.
02:19 Let us execute the program and observe what happens.
02:24 On the terminal, type - chmod +x for.sh
02:31 Then type: ./for.sh
02:36 I will enter 5 as the input number.
02:40 The sum that is calculated for each value of 'i' is displayed.
02:46 After that, the last line of the output is displayed:
02:50 "Sum of first n numbers is 15".
02:54 Now, let us see the flow of the program.
02:57 Let me resize the windows.
03:00 First, we have value of 'i' as 1.
03:04 Then we check whether 1 is less than or equal to 5.
03:10 Since the condition is True, we calculate the sum as 0 + 1.
03:16 Now we have the sum as 1.
03:20 Then we print sum i.e 1.
03:24 Next, 'i' is incremented by 1 and the new value of 'i' is 2.
03:31 Then we check whether 2 is less than or equal to 5.
03:36 The condition is True and now sum will be 1 + 2 i.e 3.
03:44 'i' is then incremented by 1 and then the new value of 'i' is 3.
03:51 And we get sum as 6.
03:55 The script will continue to add the next value of 'i' to the previous value in sum.
04:02 This will continue till i<=5 is False.
04:09 On exiting the for loop, the final message is printed.
04:14 Let us see another example of for loopusing the second syntax.
04:20 I have written the code in this file and named it as for-loop.sh.
04:27 This simple program will list the files in a directory.
04:32 This is the shebang line.
04:35 Then we have for loop.
04:37 'ls' command lists the directory content.
04:41 -1 (hyphen one) lists one file per line.
04:46 This will list all the files present in your home directory.
04:51 This is the end of for loop.
04:53 Let us execute the script on the terminal by typing -
04:58 chmod +x for-loop.sh
05:04 Type: ./for-loop.sh
05:09 This will display all the files present in the Home directory.
05:14 Now we will learn about while loop.
05:18 Let us understand the syntax first.
05:21 while condition

statement 1, 2, 3 end of while loop.

05:27 This means that the while loop will execute as long as the condition is True.
05:34 Let us see an example of while loop.
05:37 Here, I have named it as while.sh.
05:42 In this program, we will calculate the sum of even numbers within a given range.
05:49 Let us go through the code.
05:52 Here, we accept a number from the user and store it in variable number.
05:59 Next, we declare variables 'i' and sum and initialize them to 0 (zero).
06:06 Now, this is the while condition.
06:08 Here, we check whether 'i' is less than or equal to value of number given by the user.
06:17 Then we calculate the sum by adding value of 'i' to value of sum.
06:24 Next, we increment the value of 'i' with 2.
06:28 This will ensure that we only add the even numbers.
06:33 And the while loop is repeated till the value of 'i' exceeds the value of number.
06:40 When we exit the while loop, we print the sum of all the even numbers within the given range.
06:47 Let us execute the program.
06:50 Type on the terminal:
06:52 chmod +x while.sh
06:56 ./while.sh
07:00 I will give 15 as my input.
07:04 The last line of the output is:
07:06 "Sum of even numbers within the given range is 56".
07:11 Let me resize the window and explain the output.
07:14 First we check whether 'i' which is 0, is less than or equal to the number which is 15.
07:24 The condition is True. Hence sum will be '0+0' i.e 0.
07:31 Now 'i' will be incremented by 2 and the new value of 'i' is 2.
07:37 Then we check if 2 is less than or equal to 15.
07:43 The condition again is True; so we add 0+2.
07:49 Now sum has the value 2.
07:52 Again the value of 'i' will be incremented by 2.
07:56 So, now value of 'i' will be 2+2 i.e 4.
08:03 And the next value of sum will be 4+2 i.e 6.
08:09 In the same way, the script will continue to add 2 to the previous value of 'i', till it exceeds 15.
08:18 And we get the total value in sum as 56.
08:24 This brings us to the end of this tutorial.
08:27 Let us summarize. In this tutorial, we learned two different syntax of for loop and we also learned about the while loop.
08:37 As an assignment -find the sum of the first "n" prime numbers.
08:43 Watch the video available at the link shown below.
08:46 It summarizes the Spoken-Tutorial project.
08:50 If you do not have good bandwidth, you can download and watch it.
08:54 The Spoken Tutorial Project team:
08:56 Conducts workshops using spoken tutorials.
09:00 Gives certificates to those who pass an online test.
09:04 For more details, please write to contact@spoken-tutorial.org
09:11 Spoken Tutorial Project is a part of the Talk to a Teacher project.
09:14 It is supported by the National Mission on Education through ICT, MHRD, Government of India.
09:22 More information on this mission is available at the link shown below.
09:28 The script has been contributed by Fossee and spoken-tutorial team.
09:34 This is Ashwini Patil from IIT Bombay, signing off.
09:38 Thank You for joining.

Contributors and Content Editors

PoojaMoolya, Pratik kamble, Ranjana, Sandhya.np14