Difference between revisions of "C-and-C++/C3/Loops/English-timed"

From Script | Spoken-Tutorial
Jump to: navigation, search
 
(9 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
{| border = 1
 
{| border = 1
 
 
|'''Time'''
 
|'''Time'''
 
 
|'''Narration'''
 
|'''Narration'''
 
  
 
|-
 
|-
| 00.01
+
| 00:01
|Welcome to the spoken tutorial on Loops in C and C++  
+
|Welcome to the spoken tutorial on '''Loops in C and C++'''.
  
 
|-
 
|-
| 00.06
+
| 00:06
 
|In this tutorial we will learn,   
 
|In this tutorial we will learn,   
  
 
|-
 
|-
| 00.09
+
| 00:09
|for loop,  
+
|'''for loop,'''while loop''' and
  
 
|-
 
|-
| 00.10
+
| 00:12
|while loop and
+
|'''do…while loop. ''' We will do this with the help of some examples.
  
 
|-
 
|-
| 00.12
+
| 00:17
|do…while loop.
+
 
+
|-
+
| 00.13
+
| We will do this with the help of some examples.
+
 
+
|-
+
| 00.17
+
 
|We will also see some common errors and their solutions.  
 
|We will also see some common errors and their solutions.  
  
 
|-
 
|-
| 00.21
+
| 00:21
 
| To record this tutorial, I am using  
 
| To record this tutorial, I am using  
  
 
|-
 
|-
| 00.24
+
| 00:24
|Ubuntu Operating System version 11.04  
+
|'''Ubuntu Operating System''' version 11.04,
  
 
|-
 
|-
|00.28
+
|00:28
| gcc and g++ Compiler version 4.6.1 on Ubuntu.  
+
| '''gcc''' and '''g++ Compiler''' version 4.6.1 on Ubuntu.  
 
+
  
 
|-
 
|-
|00.34
+
|00:34
 
|Let us start with the introduction to loops.  
 
|Let us start with the introduction to loops.  
  
 
|-
 
|-
|00.38
+
|00:38
 
|Loops are used to execute a group of instructions repeatedly.  
 
|Loops are used to execute a group of instructions repeatedly.  
  
 
|-
 
|-
| 00.44
+
| 00:44
 
|Depending on the purpose they are divided into three types:  
 
|Depending on the purpose they are divided into three types:  
  
 
|-
 
|-
| 00.48
+
| 00:48
|while loop  
+
|'''while loop ''' '''do…..while loop''' and
  
 +
|-
 +
| 00:51
 +
|'''for loop '''Let us start with the '''while loop''' first.
  
 
|-
 
|-
| 00.49
+
| 00:56
|do…..while loop and
+
|A '''while loop''' tests the condition in the beginning.
  
 
|-
 
|-
| 00.51
+
| 01:00
|for loop
+
|The structure is: '''while''' ( condition )
  
 
|-
 
|-
| 00.52
+
|01:03
|Let us start with the while loop first
+
|within the bracket '''statement block'''.
  
 
|-
 
|-
| 00.56
+
| 01:07
|A while loop tests the condition in the beginning
+
|Now move on to  '''do….while loop '''.
  
 
|-
 
|-
| 00.59
+
| 01:09
|The structure is  
+
|A '''do..while loop''' is executed '''at least once''' before the condition could be validated.
  
 
|-
 
|-
| 01.01
+
| 01:15
|while ( condition )
+
|The structure is:
  
 
|-
 
|-
|01.03
+
| 01:17
|within the bracket statement block  
+
|'''do''' (within the brackets) statement block,
  
 
|-
 
|-
| 01.07
+
|01:20
|Now move on to  do….while loop
+
|after the bracket the '''while''' ( condition ).
  
 
|-
 
|-
| 01.09
+
|01:23
|A do..while loop is executed at least once before the condition could be validated.  
+
|You can see that the condition is checked at the end.
  
 
|-
 
|-
| 01.15
+
| 01:27
|The structure is
+
|Now,let us see an example on '''while loop''' and '''do...while loop '''.
  
 
|-
 
|-
| 01.17
+
| 01:32
|do (within the brackets) statement block
+
|I have already typed the code on the editor.
  
 +
|-
 +
| 01:35
 +
|Let me open it.
  
 
|-
 
|-
|01.20
+
|01:37
|after the bracket the while ( condition )
+
|Note that our file name is '''while.c.'''.
  
 
|-
 
|-
|01.23
+
|01:41
|You can see that the condition is checked at the end.
+
|Today we are going to learn addition of first 10 numbers using '''while''' loop.
  
 
|-
 
|-
| 01.27
+
|01:47
|Now,let us see an example on while loop and do...while loop
+
|Let me explain the code now.
  
 
|-
 
|-
| 01.32
+
|01:49
|I have already typed the code on the editor.  
+
|This is our '''header file.'''
  
 
|-
 
|-
| 01.35
+
| 01:51
|Let me open it.  
+
|Inside the '''main()''' function we have declared two '''integer variables x''' and '''y '''and initialized to 0.
  
 
|-
 
|-
|01.37
+
| 01:59
|Note that our file name is while.c.
+
| This is our '''while loop.'''
  
 
|-
 
|-
|01.41
+
| 02:02
|Today we are going to learn addition of first 10 numbers using while loop.
+
| The condition of the '''while''' loop is '''x is less than or equal to 10. '''
  
 
|-
 
|-
|01.47
+
|02:06
|Let me explain the code now.
+
|Here the value of '''x''' is added to the value of '''y.'''
  
 
|-
 
|-
|01.49
+
| 02:10
|This is our header file.
+
| The value obtained after the addition is stored in '''y. '''
  
 
|-
 
|-
| 01.51
+
| 02:15
|Inside the main function we have declared two integer variables x and y and initialized to 0.
+
| Then we print the value of y.
  
 +
|-
 +
| 02:18
 +
| Here ''' x '''is incremented.
  
 +
|-
 +
| 02:20
 +
| That means the variable '''x''' is increased by one.
  
 
|-
 
|-
| 01.59
+
| 02:25
| This is our while loop.  
+
|And this is our '''return''' statement.
  
|
 
 
|-
 
|-
| 02.02
+
| 02:27
| The condition of the while loop is x is less than or equal to 10.  
+
|Now,let us execute the program.  
  
 +
|-
 +
| 02:30
 +
| Please open the '''terminal''' window by pressing '''Ctrl, Alt''' and '''T''' keys simultaneously on your keyboard.
  
 
|-
 
|-
|02.06
+
| 02:39
|Here the value of x is added to the value of y.
+
|Type '''gcc space while dot c space hyphen o space while '''
  
 +
|-
 +
| 02:45
 +
|Press '''Enter'''.
  
 
|-
 
|-
| 02.10
+
| 02:47
| The value obtained after the addition is stored in y.  
+
|Type  '''./while ''' (dot slash while). Press Enter.
  
 
|-
 
|-
| 02.15
+
| 02:52
| Then we print the value of y
+
| The output is displayed.
  
 
|-
 
|-
| 02.18
+
| 02:54
| Here  x is incremented.  
+
| Now lets us see the working of ''' while loop.'''
  
 
|-
 
|-
| 02.20
+
| 02:57
| That means the variable x is increased by one.
+
|Let me resize the window.
  
 
|-
 
|-
| 02.25
+
| 03:00
|And this is our return statement.
+
|Here, first the  value of  '''x ''' and  '''y ''' is 0.
  
 
|-
 
|-
| 02.27
+
| 03:04
|Now,let us execute the program.  
+
|This is our  '''while ''' condition.
  
 
|-
 
|-
| 02.30
+
| 03:06
| Please open the terminal window by pressing Ctrl, Alt and T keys simultaneously on your keyboard.
+
|Here we check whether x is less than or equal to 10 which means the values of x will be from 0 to 10.
  
 
|-
 
|-
| 02.39
+
| 03:15
|Type gcc space while dot c space hypen o space while
+
|Then we add y plus x (i.e) 0 plus 0, we get 0.
  
 
|-
 
|-
| 02.45
+
| 03:22
|Press Enter
+
| We print the value of y, here we get 0.
  
 +
|-
 +
| 03:27
 +
| Then x is incremented which means now the value of x will be 1.
  
 
|-
 
|-
| 02.47
+
| 03:33
|Type ./ (dot slash) while .Press Enter
+
|Then we  will check the condition again, 1 is less than or equal to 10. If the condition is  '''true ''' then we will add the values,
  
 +
|-
 +
| 03:44
 +
| y (i.e ) 0 plus x that is 1. 0 plus 1 is 1.
  
 
|-
 
|-
| 02.52
+
| 03:50
| The output is displayed.
+
|We print the value as 1.
  
 
|-
 
|-
| 02.54
+
| 03:53
| Now lets us see the working of while loop.
+
|Again x is incremented.
  
 
|-
 
|-
| 02.57
+
| 03:55
|Let me resize the window.
+
|Now the value of x is 2.
  
 
|-
 
|-
| 03.00
+
| 03:59
|Here, first the value of x and y is 0
+
|We check the condition again.
 
+
  
 
|-
 
|-
| 03.04
+
| 04:01
|This is our while condition.Here we check whether x is less than or equal to 10 which means the values of x will be from 0 to 10
+
|2 is less than or equal to 10, if the condition is  '''true ''' then we will add the values, (i.e ) 1 plus 2 which will give 3.
  
 
|-
 
|-
| 03.15
+
| 04:11
|Then we  add y plus x (i.e)  0 plus 0 we get 0. We print the value of y, here we get 0. Then x is incremented which means now the value of x will be 1
+
|We print the value as 3.  
  
 
|-
 
|-
| 03.33
+
| 04:13
|Then we  will check the condition again, 1 is less than or equal to 10, if the condition is true then we will add the values, y (i.e ) 0 plus x that is 1. 0 plus 1 is 1
+
|Like this, it will go on up to x is less than or equal to 10 (x<=10).
  
 
|-
 
|-
| 03.50
+
| 04:20
|We print the value as 1.Again x is incremented.
+
| Now, we will see the same program using  '''do….while loop  '''.
  
 
|-
 
|-
| 03.55
+
| 04:24
|Now the value of x is 2.We check the condition again. 2 is less than or equal to 10, if the condition is true then we will add the values,(i.e ) 1 plus 2 which will give 3.
+
| Here is our program.
  
 
|-
 
|-
| 04.11
+
| 04:26
|We print the value as 3. Like this it will go on upto x is less than or equal to 10
+
|Note that our file name is '''do hyphen while dot c '''.
 +
 
 
|-
 
|-
| 04.20
+
| 04:31
| Now, we will see the same program using do….while loop
+
|This part is already explained in the previous program.  
  
 
|-
 
|-
| 04.24
+
| 04:35
| Here is our program
+
|So, let us move on to a  '''do...while loop '''.
  
 
|-
 
|-
| 04.26
+
| 04:38
|Note that our file name is do hypen while.c
+
|Here, first the body of the loop will be executed  and then the condition is checked.
  
 
|-
 
|-
| 04.31
+
| 04:44
|This part is already explained in the previous program.So lets us move on to a do...while loop
+
|The value of x is added to the value of y and the value obtained after the addition is stored in y.
  
 
|-
 
|-
| 04.38
+
| 04:52
|Here first the body of the loop will be executed  and then the condition is checked.The value of x is added to the value of y and the value obtained after the addition is stored in y
+
|The logic is same as in '''while ''' program.
  
 
|-
 
|-
| 04.52
+
| 04:55
|The logic is same as in while program
+
|Now let us execute the program.
  
 
|-
 
|-
| 04.55
+
| 04:58
|Now lets us execute the program
+
|Come back to our terminal.
  
 
|-
 
|-
| 04.58
+
| 05:00
|Come back to our terminal
+
|Type  '''gcc space do hyphen while dot c space hyphen o space do '''. Press  '''Enter '''.
  
 
|-
 
|-
| 05.00
+
| 05:08
|Type gcc space do hypen while dot c space hypen o space do.Press Enter
+
|Type '''dot slash do '''(./do). Press '''Enter '''.
  
 
|-
 
|-
| 05.08
+
| 05:12
|Type dot slash do. Press Enter
+
|We can see that the output is similar  to our  '''while ''' program.
  
 
|-
 
|-
| 05.12
+
| 05:16
|We can see that the output is similiar to our while program
+
|Now, let us see the working of '''do...while loop '''.
  
 
|-
 
|-
| 05.16
+
| 05:20
|Now, lets us see the working of do...while loop
+
|Let me resize the window.
  
 
|-
 
|-
| 05.20
+
| 05:22
|Let me resize the window
+
|Here the value x and y is 0.
  
 
|-
 
|-
| 05.22
+
| 05:25
|Here the value x and y is 0
+
|We add those values. Then we will get 0.
  
 
|-
 
|-
| 05.25
+
| 05:29
|We add those values then we will get 0
+
|Now the value of y is 0.
  
 
|-
 
|-
| 05.29
+
| 05:31
|Now the value of y is 0. We print the value as 0
+
|We print the value as 0.
  
 
|-
 
|-
| 05.33
+
| 05:33
 
|Then x is incremented by 1 which means now the value of x is 1, then the condition will be checked.
 
|Then x is incremented by 1 which means now the value of x is 1, then the condition will be checked.
  
 
|-
 
|-
| 05.40
+
| 05:42
| You can see that the body of loop is executed first. Anyhow if the condition is false then also we will get the values that is 0.
+
| You can see that the body of loop is executed first.
  
 
|-
 
|-
| 05.52
+
| 05:45
|Now, here we will check whether 1 is less than or equal to 10
+
| Anyhow, if the condition is  '''false ''' then also we will get a value that is 0.
  
 +
|-
 +
| 05:52
 +
|Now, here we will check whether 1 is less than or equal to 10.
  
 
|-
 
|-
| 05.56
+
| 05:56
|The condition is true again we will add the values. Now 0 plus 1. Then we will print the value of y as 1
+
|The condition is true. Again we will add the values.  
  
 
|-
 
|-
| 06.05
+
| 06:00
|Again x will be incremented.Now the value of x is 2. Then we check 2 is  less than or equal to 10
+
| Now 0 plus 1.  
  
 
|-
 
|-
| 06.15
+
| 06:02
|We will go back here
+
| Then we will print the value of y as 1.
  
 
|-
 
|-
| 06.17
+
| 06:05
|Then we will add the values 1 plus 2 is 3
+
|Again x will be incremented. 
  
 
|-
 
|-
| 06.20
+
| 06:08
| We print the value of y as 3
+
|Now the value of x is 2.
  
 
|-
 
|-
| 06.23
+
| 06:11
|Like this the conditions will be checked till the value of x will be less than or equal to 10
+
|Then we check 2 is  less than or equal to 10.
  
 
|-
 
|-
| 06.30
+
| 06:15
|And this is our return statement.
+
|We will go back here.
  
 
|-
 
|-
| 06.33
+
| 06:17
|Note that here the while condition ends with the semicolon
+
|Then we will add the values. 1 plus 2 is 3.
  
 
|-
 
|-
| 06.38
+
| 06:20
|In while loop the condition does not end with the semicolon.
+
| We print the value of y as 3.
  
 
|-
 
|-
| 06.43
+
| 06:23
|Now lets us see how to execute these programs in C++
+
|Like this, the conditions will be checked till the value of x will be less than or equal to 10.
  
 
|-
 
|-
| 06.48
+
| 06:30
|This is our while program in C++
+
|And this is our '''return''' statement.
  
 
|-
 
|-
| 06.52
+
| 06:33
|The logic and implementation are same as in our C program
+
|Note that here the while condition ends with the semicolon.
  
 
|-
 
|-
| 06.56
+
| 06:38
|There are a few changes like the header file as iostream in place of stdio.h
+
|In '''while loop''' the condition does not end with the semicolon.
  
 +
|-
 +
| 06:43
 +
|Now let us see how to execute these programs in C++.
  
 
|-
 
|-
| 07.04
+
| 06:48
|We have included the using statement here using namespace std and here we have use the cout function in place of printf function
+
|This is our while program in C++.
  
 
|-
 
|-
| 07.16
+
| 06:52
|The structure of while loop is same as in our C program.
+
|The logic and implementation are same as in our C program.
  
 
|-
 
|-
| 07.21
+
| 06:56
|Lets us execute the program
+
|There are a few changes like the header file as '''iostream''' in place of '''stdio.h'''.
  
 
|-
 
|-
| 07.23
+
| 07:04
|Come back to a terminal
+
|We have included the 'using' statement here '''using namespace std''' and here we have used the '''cout''' function in place of  '''printf ''' function.
  
 
|-
 
|-
| 07.25
+
| 07:16
|Let me clear the prompt
+
|The structure of  '''while''' loop is same as in our C program.
  
 +
|-
 +
| 07:21
 +
|Lets us execute the program.
  
 
|-
 
|-
| 07.28
+
| 07:23
|To execute type g++ space do , g++ space while dot cpp space hypen o space while1. Press Enter
+
|Come back to a terminal.
  
 
|-
 
|-
| 07.41
+
| 07:25
|Type dot slash while1.Press Enter
+
|Let me clear the prompt.
  
 
|-
 
|-
| 07.46
+
| 07:28
|You can see that the output is similiar to our while program in C.
+
|To execute, type  ''' g++ space while dot cpp space hyphen o space while1 '''. Press '''Enter '''.
  
 
|-
 
|-
| 07.51
+
| 07:38
|Now let us see the do... while program in C++
+
|Type  '''dot slash while1 '''(./while1). Press  '''Enter '''.
  
 
|-
 
|-
| 07.55
+
| 07:43
|Come back to the Text editor
+
|You can see that the output is similar to our while program in C.
  
 
|-
 
|-
| 07.57
+
| 07:48
|Here also there are similiar changes like the header file,the using statement and the cout function
+
|Now let us see the '''do... while ''' program in C++.
  
 
|-
 
|-
| 08.06
+
| 07:52
|Rest of the things are similiar
+
|Come back to the text editor.
  
 
|-
 
|-
| 08.09
+
| 07:54
|Lets us execute the program.Come back to our terminal
+
|Here also there are similar changes like the  '''header file ''', the  '''using statement ''' and the '''cout  '''function.
  
 
|-
 
|-
| 08.13
+
| 08:03
|Type g++ space do hypen while dot cpp space hypen o space do1. Press Enter
+
|Rest of the things are similar.
  
 
|-
 
|-
| 08.23
+
| 08:06
|Type dot slash do1.Press Enter
+
|Lets us execute the program.
  
 
|-
 
|-
| 08.26
+
| 08:08
|We can see that the output is similiar to our do...while program in C.
+
|Come back to our terminal.
  
 
|-
 
|-
| 08.32
+
| 08:10
|Now we will see some common errors and their solutions
+
|Type  '''g++ space do hyphen while dot cpp space hyphen o space do1 '''. Press  '''Enter '''.
  
 
|-
 
|-
| 08.35
+
| 08:19
|Come back to our text editor
+
|Type  '''dot slash do1 '''(./do1). Press  '''Enter '''.
  
 
|-
 
|-
| 08.38
+
| 08:23
|Suppose here I will not increment the value of x.Click on Save. Let us see what happens
+
|We can see that the output is similar to our  '''do...while program in C. '''
  
 
|-
 
|-
| 08.46
+
| 08:28
|Come back to the terminal. Let me clear the prompt
+
|Now we will see some common errors and their solutions.
  
 
|-
 
|-
| 08.50
+
| 08:32
|Lets us execute the program. Press the uparrow key twice. Again press the uparrow key
+
|Come back to our text editor.
  
 
|-
 
|-
| 09.00
+
| 08:35
|The output is displayed. We can see number of zeros, this is because the loop does not have the terminating condition . It is known as infinite loop
+
|Suppose, here I will not increment the value of x.  
  
 +
|-
 +
| 08:41
 +
|Click on Save Let us see what happens.
  
 
|-
 
|-
| 09.13
+
| 08:44
|Infinite loop can cause the system to become unresponsive. It causes the program to consume all the prossess time but it can be terminated
+
|Come back to the terminal Let me clear the prompt.
  
 
|-
 
|-
| 09.25
+
| 08:47
|Come back to our program let us fix the error. Type x++ and a semicolon.
+
|Lets us execute the program.  
  
 
|-
 
|-
| 09.31
+
| 08:50
|Click on Save. Lte us execute again. Come back to terminal.
+
|Press the up-arrow key twice.
 +
|-
 +
| 08:54
 +
| Again press the up-arrow key.
  
 
|-
 
|-
| 09.36
+
| 08:57
|Press the uparrow key
+
|The output is displayed.
  
 
|-
 
|-
| 09.41
+
| 08:59
|Yes, it is working
+
|We can see number of zeros, this is because the loop does not have the terminating condition .
  
 
|-
 
|-
| 09.43
+
| 09:07
|This bring us to the end of the tutorial. We will move back to our slides.
+
|It is known as  '''infinite loop. '''
  
 
|-
 
|-
| 09.49
+
| 09:10
|Let us summarize
+
|'''Infinite loop ''' can cause the system to become unresponsive.
  
 +
|-
 +
| 09:14
 +
| It causes the program to consume all the  '''processors  time ''' but it can be terminated.
  
 
|-
 
|-
| 09.50
+
| 09:21
|In this tutorial we learned,  
+
|Come back to our program, let us fix the error.
  
 
|-
 
|-
| 09.52
+
| 09:25
|while loop
+
|Type  '''x++ ''' and a semicolon.
  
 
|-
 
|-
| 09.53
+
| 09:28
|example. while(x is less than or equal to10)
+
|Click on Save. Let us execute again.  
  
 
|-
 
|-
| 09.57
+
| 09:31
|do….while loop
+
|Come back to terminal.
  
 
|-
 
|-
| 09.58
+
| 09:33
|example. do statement block and
+
|Press the up-arrow key.
  
 +
|-
 +
| 09:38
 +
|Yes, it is working.
  
 
|-
 
|-
| 10.02
+
| 09:40
|while condition at the end
+
|This brings us to the end of this tutorial.
  
 
|-
 
|-
| 10.04
+
| 09:43
|As an assignment
+
|We will move back to our slides.
  
 
|-
 
|-
| 10.06
+
| 09:45
|Write a program to print the following using for loops
+
|Let us summarize.
  
 
|-
 
|-
| 10.10
+
| 09:47
|0 to 9
+
|In this tutorial we learned,
  
 
|-
 
|-
| 10.12
+
| 09:50
|The syntax of the for loop is  
+
| '''while loop ''' example. while(x is less than or equal to 10)
  
 
|-
 
|-
| 10.15
+
| 09:54
|for (variable initialization; variable condition;and variable increment or decrement)
+
| '''do….while loop  '''.
  
 
|-
 
|-
| 10.24
+
| 09:56
|And here will be the body of the loop
+
|example. do statement block and
  
 +
|-
 +
| 09:59
 +
|while condition at the end.
  
 +
|-
 +
| 10:01
 +
|As an assignment,
  
 +
|-
 +
| 10:03
 +
|write a program to print the following, using '''for''' loops.
  
 +
|-
 +
| 10:07
 +
|0 to 9.
 +
 +
|-
 +
| 10:10
 +
|The syntax of the  '''for loop ''' is
 +
 +
|-
 +
|10:12
 +
|'''for (variable initialization; variable condition; and variable increment or decrement) '''
 +
 +
|-
 +
| 10:20
 +
|and here will be the body of the loop.
  
 
|-
 
|-
| 10.27
+
| 10:24
|Watch the video available at the link shown below
+
|Watch the video available at the link shown below.
  
 
|-
 
|-
| 10.30
+
| 10:27
|It summarizes the Spoken Tutorial project  
+
|It summarizes the Spoken Tutorial project.
  
 
|-
 
|-
| 10.32
+
| 10:30
|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.
  
 
|-
 
|-
| 10.36
+
| 10:33
|The Spoken Tutorial Project Team  
+
|The Spoken Tutorial Project Team:
  
 
|-
 
|-
| 10.38
+
| 10:35
|Conducts workshops using spoken tutorials  
+
|Conducts workshops using spoken tutorials.
  
 
|-
 
|-
| 10.41
+
| 10:38
|Gives certificates to those who pass an online test  
+
|Gives certificates to those who pass an online test.
  
 
|-
 
|-
| 10.45
+
| 10:42
|For more details, please write to, contact@spoken-tutorial.org  
+
|For more details, please write to contact@spoken-tutorial.org.
  
 
|-
 
|-
| 10.50
+
| 10:47
|Spoken Tutorial Project is a part of Talk to a Teacher project  
+
|Spoken Tutorial Project is a part of Talk to a Teacher project.
  
 
|-
 
|-
| 10.55
+
| 10:51
|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.01
+
| 10:58
|More information on this Mission is available at the link shown below
+
|More information on this mission is available at the link shown below.
  
 
|-
 
|-
| 11.05
+
| 11:02
|This script as been contributed by Dhawal Goyal. This is Ashwini Patil from IIT Bombay signing off  
+
|This script as been contributed by Dhawal Goyal. This is Ashwini Patil from IIT Bombay, signing off.
  
 
|-
 
|-
| 11.12
+
| 11:08
 
|Thank You for joining.
 
|Thank You for joining.

Latest revision as of 14:23, 24 March 2017

Time Narration
00:01 Welcome to the spoken tutorial on Loops in C and C++.
00:06 In this tutorial we will learn,
00:09 for loop,while loop and
00:12 do…while loop. We will do this with the help of some examples.
00:17 We will also see some common errors and their solutions.
00:21 To record this tutorial, I am using
00:24 Ubuntu Operating System version 11.04,
00:28 gcc and g++ Compiler version 4.6.1 on Ubuntu.
00:34 Let us start with the introduction to loops.
00:38 Loops are used to execute a group of instructions repeatedly.
00:44 Depending on the purpose they are divided into three types:
00:48 while loop do…..while loop and
00:51 for loop Let us start with the while loop first.
00:56 A while loop tests the condition in the beginning.
01:00 The structure is: while ( condition )
01:03 within the bracket statement block.
01:07 Now move on to do….while loop .
01:09 A do..while loop is executed at least once before the condition could be validated.
01:15 The structure is:
01:17 do (within the brackets) statement block,
01:20 after the bracket the while ( condition ).
01:23 You can see that the condition is checked at the end.
01:27 Now,let us see an example on while loop and do...while loop .
01:32 I have already typed the code on the editor.
01:35 Let me open it.
01:37 Note that our file name is while.c..
01:41 Today we are going to learn addition of first 10 numbers using while loop.
01:47 Let me explain the code now.
01:49 This is our header file.
01:51 Inside the main() function we have declared two integer variables x and y and initialized to 0.
01:59 This is our while loop.
02:02 The condition of the while loop is x is less than or equal to 10.
02:06 Here the value of x is added to the value of y.
02:10 The value obtained after the addition is stored in y.
02:15 Then we print the value of y.
02:18 Here x is incremented.
02:20 That means the variable x is increased by one.
02:25 And this is our return statement.
02:27 Now,let us execute the program.
02:30 Please open the terminal window by pressing Ctrl, Alt and T keys simultaneously on your keyboard.
02:39 Type gcc space while dot c space hyphen o space while
02:45 Press Enter.
02:47 Type ./while (dot slash while). Press Enter.
02:52 The output is displayed.
02:54 Now lets us see the working of while loop.
02:57 Let me resize the window.
03:00 Here, first the value of x and y is 0.
03:04 This is our while condition.
03:06 Here we check whether x is less than or equal to 10 which means the values of x will be from 0 to 10.
03:15 Then we add y plus x (i.e) 0 plus 0, we get 0.
03:22 We print the value of y, here we get 0.
03:27 Then x is incremented which means now the value of x will be 1.
03:33 Then we will check the condition again, 1 is less than or equal to 10. If the condition is true then we will add the values,
03:44 y (i.e ) 0 plus x that is 1. 0 plus 1 is 1.
03:50 We print the value as 1.
03:53 Again x is incremented.
03:55 Now the value of x is 2.
03:59 We check the condition again.
04:01 2 is less than or equal to 10, if the condition is true then we will add the values, (i.e ) 1 plus 2 which will give 3.
04:11 We print the value as 3.
04:13 Like this, it will go on up to x is less than or equal to 10 (x<=10).
04:20 Now, we will see the same program using do….while loop .
04:24 Here is our program.
04:26 Note that our file name is do hyphen while dot c .
04:31 This part is already explained in the previous program.
04:35 So, let us move on to a do...while loop .
04:38 Here, first the body of the loop will be executed and then the condition is checked.
04:44 The value of x is added to the value of y and the value obtained after the addition is stored in y.
04:52 The logic is same as in while program.
04:55 Now let us execute the program.
04:58 Come back to our terminal.
05:00 Type gcc space do hyphen while dot c space hyphen o space do . Press Enter .
05:08 Type dot slash do (./do). Press Enter .
05:12 We can see that the output is similar to our while program.
05:16 Now, let us see the working of do...while loop .
05:20 Let me resize the window.
05:22 Here the value x and y is 0.
05:25 We add those values. Then we will get 0.
05:29 Now the value of y is 0.
05:31 We print the value as 0.
05:33 Then x is incremented by 1 which means now the value of x is 1, then the condition will be checked.
05:42 You can see that the body of loop is executed first.
05:45 Anyhow, if the condition is false then also we will get a value that is 0.
05:52 Now, here we will check whether 1 is less than or equal to 10.
05:56 The condition is true. Again we will add the values.
06:00 Now 0 plus 1.
06:02 Then we will print the value of y as 1.
06:05 Again x will be incremented.
06:08 Now the value of x is 2.
06:11 Then we check 2 is less than or equal to 10.
06:15 We will go back here.
06:17 Then we will add the values. 1 plus 2 is 3.
06:20 We print the value of y as 3.
06:23 Like this, the conditions will be checked till the value of x will be less than or equal to 10.
06:30 And this is our return statement.
06:33 Note that here the while condition ends with the semicolon.
06:38 In while loop the condition does not end with the semicolon.
06:43 Now let us see how to execute these programs in C++.
06:48 This is our while program in C++.
06:52 The logic and implementation are same as in our C program.
06:56 There are a few changes like the header file as iostream in place of stdio.h.
07:04 We have included the 'using' statement here using namespace std and here we have used the cout function in place of printf function.
07:16 The structure of while loop is same as in our C program.
07:21 Lets us execute the program.
07:23 Come back to a terminal.
07:25 Let me clear the prompt.
07:28 To execute, type g++ space while dot cpp space hyphen o space while1 . Press Enter .
07:38 Type dot slash while1 (./while1). Press Enter .
07:43 You can see that the output is similar to our while program in C.
07:48 Now let us see the do... while program in C++.
07:52 Come back to the text editor.
07:54 Here also there are similar changes like the header file , the using statement and the cout function.
08:03 Rest of the things are similar.
08:06 Lets us execute the program.
08:08 Come back to our terminal.
08:10 Type g++ space do hyphen while dot cpp space hyphen o space do1 . Press Enter .
08:19 Type dot slash do1 (./do1). Press Enter .
08:23 We can see that the output is similar to our do...while program in C.
08:28 Now we will see some common errors and their solutions.
08:32 Come back to our text editor.
08:35 Suppose, here I will not increment the value of x.
08:41 Click on Save Let us see what happens.
08:44 Come back to the terminal Let me clear the prompt.
08:47 Lets us execute the program.
08:50 Press the up-arrow key twice.
08:54 Again press the up-arrow key.
08:57 The output is displayed.
08:59 We can see number of zeros, this is because the loop does not have the terminating condition .
09:07 It is known as infinite loop.
09:10 Infinite loop can cause the system to become unresponsive.
09:14 It causes the program to consume all the processors time but it can be terminated.
09:21 Come back to our program, let us fix the error.
09:25 Type x++ and a semicolon.
09:28 Click on Save. Let us execute again.
09:31 Come back to terminal.
09:33 Press the up-arrow key.
09:38 Yes, it is working.
09:40 This brings us to the end of this tutorial.
09:43 We will move back to our slides.
09:45 Let us summarize.
09:47 In this tutorial we learned,
09:50 while loop example. while(x is less than or equal to 10)
09:54 do….while loop .
09:56 example. do statement block and
09:59 while condition at the end.
10:01 As an assignment,
10:03 write a program to print the following, using for loops.
10:07 0 to 9.
10:10 The syntax of the for loop is
10:12 for (variable initialization; variable condition; and variable increment or decrement)
10:20 and here will be the body of the loop.
10:24 Watch the video available at the link shown below.
10:27 It summarizes the Spoken Tutorial project.
10:30 If you do not have good bandwidth, you can download and watch it.
10:33 The Spoken Tutorial Project Team:
10:35 Conducts workshops using spoken tutorials.
10:38 Gives certificates to those who pass an online test.
10:42 For more details, please write to contact@spoken-tutorial.org.
10:47 Spoken Tutorial Project is a part of Talk to a Teacher project.
10:51 It is supported by the National Mission on Education through ICT, MHRD, Government of India.
10:58 More information on this mission is available at the link shown below.
11:02 This script as been contributed by Dhawal Goyal. This is Ashwini Patil from IIT Bombay, signing off.
11:08 Thank You for joining.

Contributors and Content Editors

PoojaMoolya, Pratik kamble, Priyacst, Sandhya.np14