Difference between revisions of "BASH/C2/Conditional-Loops/English-timed"
From Script | Spoken-Tutorial
PoojaMoolya (Talk | contribs) |
|||
(4 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
− | |||
− | |||
− | |||
− | |||
{| border=1 | {| border=1 | ||
− | + | |'''Time''' | |
− | + | |'''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''' |
|- | |- | ||
| 00:11 | | 00:11 | ||
− | | | + | |'''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 ''' |
|- | |- | ||
| 00:22 | | 00:22 | ||
− | | | + | |'''GNU BASH''' version 4.1.10 |
|- | |- | ||
| 00:26 | | 00:26 | ||
− | | Please note,'''GNU bash''' version | + | | 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. |
|- | |- | ||
Line 59: | Line 55: | ||
|- | |- | ||
| 00:51 | | 00:51 | ||
− | | | + | | and this is the end of '''for loop'''. |
|- | |- | ||
Line 75: | Line 71: | ||
|- | |- | ||
| 01:06 | | 01:06 | ||
− | | | + | | and end of '''for loop'''. |
|- | |- | ||
| 01:09 | | 01:09 | ||
− | | | + | | Let us see an example of '''for loop''' using the first syntax. |
|- | |- | ||
Line 86: | Line 82: | ||
|- | |- | ||
| 01:20 | | 01:20 | ||
− | | Note that our filename is '''for.sh''' | + | | Note that our filename is '''for.sh'''. |
|- | |- | ||
Line 94: | Line 90: | ||
|- | |- | ||
| 01:28 | | 01:28 | ||
− | | The variable '''number''' will store the value given by user. | + | | The variable '''number''' will store the '''value''' given by user. |
|- | |- | ||
Line 102: | Line 98: | ||
|- | |- | ||
| 01:37 | | 01:37 | ||
− | | Now, we initialize the variable '''sum''' as zero. | + | | Now, we initialize the '''variable''' '''sum''' as zero. |
|- | |- | ||
Line 110: | Line 106: | ||
|- | |- | ||
| 01:45 | | 01:45 | ||
− | | First, we initialize | + | | First, we initialize 'i' to 1. |
|- | |- | ||
| 01:48 | | 01:48 | ||
− | | Then we check whether | + | | 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 | + | | Now here, we calculate the '''sum''' as '''sum plus i''' |
|- | |- | ||
Line 126: | Line 122: | ||
|- | |- | ||
| 02:03 | | 02:03 | ||
− | | After this, we increment the | + | | After this, we increment the '''value''' of 'i' by 1. |
|- | |- | ||
| 02:08 | | 02:08 | ||
− | | And then we check the condition till this condition is ''' | + | | And then we check the '''condition''' till this '''condition''' is '''False'''. |
|- | |- | ||
| 02:14 | | 02:14 | ||
Line 137: | Line 133: | ||
|- | |- | ||
| 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''' |
|- | |- | ||
Line 149: | Line 145: | ||
|- | |- | ||
| 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 ' | + | | 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 | + | | 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 ''' | + | | 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.''' |
|- | |- | ||
Line 193: | Line 189: | ||
|- | |- | ||
| 03:24 | | 03:24 | ||
− | | Next, ' | + | | Next, 'i' is incremented by '''1''' and the new value of 'i' is '''2.''' |
|- | |- | ||
Line 201: | Line 197: | ||
|- | |- | ||
| 03:36 | | 03:36 | ||
− | | The condition is ''' | + | | 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.''' |
|- | |- | ||
Line 213: | Line 209: | ||
|- | |- | ||
| 03:55 | | 03:55 | ||
− | | The script will continue to add the next value of ' | + | | 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 | + | | This will continue till '''<nowiki>i<=5</nowiki>''' is '''False.''' |
|- | |- | ||
Line 229: | Line 225: | ||
|- | |- | ||
| 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'''. |
|- | |- | ||
Line 241: | Line 237: | ||
|- | |- | ||
| 04:35 | | 04:35 | ||
− | | Then we have '''for | + | | Then we have '''for''' loop. |
|- | |- | ||
| 04:37 | | 04:37 | ||
− | | '''ls''' command lists the directory content. | + | | ''' 'ls' ''' '''command''' lists the directory content. |
|- | |- | ||
Line 253: | Line 249: | ||
|- | |- | ||
| 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'''. |
|- | |- | ||
Line 261: | Line 257: | ||
|- | |- | ||
| 04:53 | | 04:53 | ||
− | | Let us execute the script on the '''terminal''' by typing - | + | | Let us '''execute''' the '''script''' on the '''terminal''' by typing - |
|- | |- | ||
Line 269: | Line 265: | ||
|- | |- | ||
| 05:04 | | 05:04 | ||
− | | '''./for-loop.sh''' | + | | Type: '''./for-loop.sh''' |
|- | |- | ||
Line 277: | Line 273: | ||
|- | |- | ||
| 05:14 | | 05:14 | ||
− | | Now | + | | Now we will learn about ''' while loop.''' |
|- | |- | ||
Line 287: | Line 283: | ||
| '''while condition''' | | '''while condition''' | ||
'''statement 1, 2, 3''' | '''statement 1, 2, 3''' | ||
− | + | end of '''while loop'''. | |
|- | |- | ||
| 05:27 | | 05:27 | ||
− | | This means that the '''while | + | | This means that the '''while''' loop will execute as long as the '''condition''' is '''True.''' |
|- | |- | ||
Line 299: | Line 295: | ||
|- | |- | ||
| 05:37 | | 05:37 | ||
− | | Here I have named it as '''while.sh''' | + | | Here, I have named it as '''while.sh'''. |
|- | |- | ||
Line 306: | Line 302: | ||
|- | |- | ||
− | |05: 49 | + | |05:49 |
− | | | + | | Let us go through the '''code'''. |
|- | |- | ||
Line 315: | Line 311: | ||
|- | |- | ||
|05:59 | |05:59 | ||
− | | Next, we declare variables | + | | 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 | + | | 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 | + | | Then we calculate the '''sum''' by adding value of 'i' to value of '''sum'''. |
|- | |- | ||
| 06:24 | | 06:24 | ||
− | | Next, we increment the value of | + | | Next, we increment the value of 'i' with '''2'''. |
|- | |- | ||
Line 339: | Line 335: | ||
|- | |- | ||
| 06:33 | | 06:33 | ||
− | | And the while loop is repeated till the value of | + | | And the '''while loop''' is repeated till the value of 'i' exceeds the value of '''number.''' |
|- | |- | ||
| 06:40 | | 06:40 | ||
Line 346: | Line 342: | ||
|- | |- | ||
| 06:47 | | 06:47 | ||
− | | Let us execute the program. | + | | Let us '''execute''' the program. |
|- | |- | ||
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 |
− | | 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 | + | | 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 ''' | + | | The '''condition''' is '''True.''' Hence '''sum''' will be '0+0' i.e 0. |
|- | |- | ||
| 07:31 | | 07:31 | ||
− | | Now | + | | Now 'i' will be incremented by '''2''' and the new value of 'i' is ''' 2.''' |
|- | |- | ||
Line 394: | Line 390: | ||
|- | |- | ||
| 07:43 | | 07:43 | ||
− | | The condition again is ''' | + | | The condition again is '''True;''' so we add '''0+2.''' |
|- | |- | ||
Line 402: | Line 398: | ||
|- | |- | ||
| 07:52 | | 07:52 | ||
− | | Again the value of | + | | Again the value of 'i' will be incremented by '''2'''. |
|- | |- | ||
| 07:56 | | 07:56 | ||
− | | So now value of | + | | 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''' | + | | 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 | + | | 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:37 | | 08:37 | ||
− | | | + | | As an assignment -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 | + | | 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 | + | | 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 |
− | | | + | |'''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 | + | | It is supported by the National Mission on Education through ICT, MHRD, Government of India. |
|- | |- | ||
| 09:22 | | 09:22 | ||
− | | More information on this | + | | 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 | + | | 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. |