Difference between revisions of "C-and-C++/C3/Loops/English"
(Created page with ''''Title of script''': Loops in C and C++ '''Author: '''Dhawal Goyal '''Keywords: Loops, for loop, while loop, do....while loop, type casting, and Video tutorial''' {| style…') |
|||
(9 intermediate revisions by 2 users not shown) | |||
Line 22: | Line 22: | ||
| style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| In this tutorial we will learn, | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| In this tutorial we will learn, | ||
− | for loop | + | *'''for''' loop |
− | while loop and | + | *'''while''' loop and |
− | do…while loop | + | *'''do…while''' loop |
− | We will do this with the help of examples. | + | We will do this with the help of some examples. |
We will also see some common errors and their solutions. | We will also see some common errors and their solutions. | ||
Line 38: | Line 38: | ||
| style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| To record this tutorial, I am using | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| To record this tutorial, I am using | ||
− | + | *'''Ubuntu Operating System''' version 11.04 | |
− | '''Ubuntu Operating System''' version 11.04 | + | *'''gcc''' and '''g++''' '''Compiler''' version 4.6.1 on '''Ubuntu'''. |
− | + | ||
− | '''gcc''' and '''g++''' '''Compiler''' version 4.6.1 on Ubuntu. | + | |
|- | |- | ||
| style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.035cm;padding-right:0.035cm;"| Slide 4 | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.035cm;padding-right:0.035cm;"| Slide 4 | ||
− | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| | + | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| Let us start with the introduction to loops. |
Loops are used to execute a group of instructions repeatedly. | Loops are used to execute a group of instructions repeatedly. | ||
Line 53: | Line 51: | ||
| style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| Depending on the purpose they are divided into three types: | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| Depending on the purpose they are divided into three types: | ||
− | '''while''' loop | + | *'''while''' loop |
− | '''do…..while '''loop | + | *'''do…..while '''loop and |
− | '''for''' loop | + | *'''for''' loop |
|- | |- | ||
| style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.035cm;padding-right:0.035cm;"| Slide 6 | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.035cm;padding-right:0.035cm;"| Slide 6 | ||
− | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| Let us start with the '''while''' loop | + | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| Let us start with the '''while''' loop first. |
− | A '''while''' loop tests the condition in the beginning | + | A '''while''' loop tests the condition in the beginning. |
− | The structure is | + | The structure is- |
'''while''' ( condition ) | '''while''' ( condition ) | ||
− | { | + | { (within the brackets) |
statement block | statement block | ||
Line 81: | Line 79: | ||
A '''do..while '''loop is executed at least once before the condition could be validated. | A '''do..while '''loop is executed at least once before the condition could be validated. | ||
− | The structure is | + | The structure is- |
− | '''do''' { | + | '''do''' { (within the brackets) |
statement block | statement block | ||
− | } '''while''' ( condition )'''<nowiki>;</nowiki>''' | + | } (after the brackets) |
+ | |||
+ | the '''while''' ( condition )'''<nowiki>;</nowiki>''' | ||
+ | |||
+ | |||
+ | You can see that the condition is checked at the end. | ||
|- | |- | ||
| style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.035cm;padding-right:0.035cm;"| | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.035cm;padding-right:0.035cm;"| | ||
− | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| | + | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| Now let us see an example on '''while''' and '''do...while''' loop. |
I have already typed the code on the editor. | I have already typed the code on the editor. | ||
− | + | Let me open it. | |
|- | |- | ||
| style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.035cm;padding-right:0.035cm;"| Point the cursor | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.035cm;padding-right:0.035cm;"| Point the cursor | ||
− | ''' | + | '''while.c''' |
− | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| | + | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| Note that our filename is '''while.c''' |
− | Today we are going to learn addition of first 10 numbers. | + | Today we are going to learn addition of first 10 numbers using '''while''' loop. |
+ | |||
+ | Let me explain the code now. | ||
|- | |- | ||
Line 115: | Line 120: | ||
'''int main()''' | '''int main()''' | ||
− | |||
− | + | '''{''' | |
− | + | ||
'''int x=0;''' | '''int x=0;''' | ||
'''int y=0;''' | '''int y=0;''' | ||
− | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| | + | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| Inside the '''main '''function we have declared two '''integer variables x''' and '''y'''. |
− | + | And initialized to 0. | |
− | + | ||
− | + | ||
|- | |- | ||
Line 141: | Line 142: | ||
'''}''' | '''}''' | ||
− | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| This is | + | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| This is our '''while''' loop. |
|- | |- | ||
| style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.035cm;padding-right:0.035cm;"| Highlight '''<nowiki>while(x<=10)</nowiki>''' | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.035cm;padding-right:0.035cm;"| Highlight '''<nowiki>while(x<=10)</nowiki>''' | ||
− | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| The condition of the while loop is x is less than or equal to 10 | + | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| The condition of the '''while''' loop is '''x is less than or equal to 10''' |
|- | |- | ||
| style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.035cm;padding-right:0.035cm;"| Highlight '''y+=x;''' | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.035cm;padding-right:0.035cm;"| Highlight '''y+=x;''' | ||
+ | |||
+ | '''printf( "%d\n", y );''' | ||
| style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| Here the value of '''x''' is added to the value of '''y.''' | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| Here the value of '''x''' is added to the value of '''y.''' | ||
The value obtained after the addition is stored in '''y'''. | The value obtained after the addition is stored in '''y'''. | ||
+ | |||
+ | Then we print the value of '''y.''' | ||
|- | |- | ||
| style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.035cm;padding-right:0.035cm;"| Highlight '''x++;''' | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.035cm;padding-right:0.035cm;"| Highlight '''x++;''' | ||
− | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| Here | + | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| Here '''x''' is incremented. |
− | + | That means the variable '''x''' is increased by one. | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
Line 173: | Line 168: | ||
'''return 0;''' | '''return 0;''' | ||
− | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| | + | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| And this is our '''return statement.''' |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
| style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.035cm;padding-right:0.035cm;"| | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.035cm;padding-right:0.035cm;"| | ||
− | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| | + | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| Now let us execute the program. |
|- | |- | ||
Line 187: | Line 178: | ||
'''Ctrl, Alt and T''' keys simultaneously | '''Ctrl, Alt and T''' keys simultaneously | ||
− | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| | + | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| Please open the '''terminal''' by pressing '''Ctrl, Alt and T''' keys simultaneously on your keyboard. |
|- | |- | ||
| style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.035cm;padding-right:0.035cm;"| Type | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.035cm;padding-right:0.035cm;"| Type | ||
− | '''gcc | + | '''gcc while.c -o while''' |
− | + | ||
− | + | ||
Type | Type | ||
− | '''./ | + | '''./while''' |
− | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| | + | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| Type |
− | '''gcc | + | '''gcc space while.c space -o space while''' |
− | + | Press '''Enter'''. | |
− | Type | + | Type '''./while''' |
− | ''' | + | Press '''Enter'''. |
|- | |- | ||
Line 213: | Line 202: | ||
'''Output 55''' | '''Output 55''' | ||
− | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| | + | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| The output is displayed. |
− | |||
|- | |- | ||
− | | style="border-top: | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.035cm;padding-right:0.035cm;"| |
− | | style="border-top: | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| Now let us see the working of '''while''' loop. |
− | + | Let me resize the windows. | |
− | + | ||
− | + | ||
|- | |- | ||
− | | style="border-top: | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.035cm;padding-right:0.035cm;"| Highlight the output. |
− | | style="border-top: | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| Here 1st the value of '''x''' and''' y''' is 0. |
|- | |- | ||
− | | style="border-top: | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.035cm;padding-right:0.035cm;"| Highlight the output. |
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| This is our '''while''' condition. | ||
− | ''' | + | Here we check whether the value of '''x''' is less than or equal to 10. |
− | ''' | + | Which means the values of '''x''' will be from 0 to 10. |
− | ''' | + | Then we add '''y + x''' ie 0 + 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. | |
− | + | ||
− | + | 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''' ie 0; ''' + x''' ie 1. |
− | + | ||
+ | 0+1 is 1. | ||
+ | We print the value as 1. | ||
+ | Again '''x''' is incremented. | ||
− | + | 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 | |
+ | |||
+ | ie 1+2 which will give 3. | ||
+ | |||
+ | We print the value as 3. | ||
+ | |||
+ | Like this it will go on upto '''x''' is less than or equal to 10. | ||
|- | |- | ||
− | | style=" | + | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.035cm;padding-right:0.035cm;"| |
+ | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| Now, we will see the same program using '''do….while''' loop | ||
+ | |- | ||
+ | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.035cm;padding-right:0.035cm;"| Point the cursor to the filename. | ||
+ | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| Here is our program. Note that our fielname is''' do-while.c''' | ||
+ | |- | ||
+ | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.035cm;padding-right:0.035cm;"| Highlight the part. | ||
+ | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| This part is already explained in the previous program. | ||
− | + | So let us move on to our '''do-while''' loop. | |
+ | Here first the body of the loop will be executed and then the condition is checked. | ||
+ | |- | ||
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.035cm;padding-right:0.035cm;"| Highlight | ||
+ | '''y+=x;''' | ||
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| The value of '''x''' is added to the value of '''y''' | ||
+ | |||
+ | The value obtained after the addition is stored in '''y'''. | ||
|- | |- | ||
− | | style=" | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.035cm;padding-right:0.035cm;"| |
− | | style=" | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| The logic is same as in '''while''' program. |
+ | Now let us execute the program. | ||
+ | |- | ||
+ | | style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0cm;"| On the terminal. | ||
+ | | style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0cm;"| Come back to our '''terminal'''. | ||
Line 288: | Line 301: | ||
| style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.035cm;padding-right:0.035cm;"| '''Switch to terminal ''' | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.035cm;padding-right:0.035cm;"| '''Switch to terminal ''' | ||
− | + | Type: | |
− | + | ||
− | + | '''gcc space do-while.c space -o space do''' | |
− | + | ||
− | + | ||
− | + | Type: | |
− | + | '''./do''' | |
− | | style="border-top: | + | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| Type: '''gcc space do-while.c space -o space do''' |
− | + | ||
+ | Press '''Enter'''. | ||
+ | |||
+ | Type: '''./do''' | ||
+ | |||
+ | Press '''Enter'''. | ||
|- | |- | ||
− | | style="border-top: | + | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.035cm;padding-right:0.035cm;"| Highlight |
− | + | ||
− | + | '''Output''' | |
+ | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| We can see that the output is similar to our while program. | ||
− | + | Now let us see the working of '''do-while''' loop. | |
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.035cm;padding-right:0.035cm;"| | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.035cm;padding-right:0.035cm;"| | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| Let me resize the windows. |
− | Here | + | |- |
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.035cm;padding-right:0.035cm;"| Highlight the output. | ||
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| Here the value of '''x''' and '''y''' is 0. | ||
− | + | We add those values. | |
− | + | Then we will get 0. | |
− | + | Now the value of '''y''' is 0 | |
− | + | We print the value as 0. | |
− | + | Then '''x''' is incremented by 1. | |
− | + | ||
− | + | ||
− | + | Which means now the value of '''x''' is 1. | |
− | + | Then the condition will be checked. | |
− | + | You can see that the body of the loop is executed first. | |
− | + | ||
− | + | ||
− | + | Anyhow if the condition is '''false''', then also we will get a value ie 0. | |
− | + | ||
− | + | Now here we will check whether 1 is less than or equal to 10. | |
− | + | ||
− | + | The condition is '''true''', again we will add the values. | |
− | + | ||
− | + | Now 0+1 | |
− | + | ||
− | + | Then we print the value of '''y''' as 1. | |
− | + | ||
− | + | ||
|- | |- | ||
− | | style="border-top: | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.035cm;padding-right:0.035cm;"| Highlight the output. |
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| Again, '''x''' will be incremented. | ||
− | ''' | + | Now the value of '''x''' is 2. |
− | + | Then we check 2 is less than or equal to 10. | |
− | + | ||
− | + | We will go back here. | |
− | + | ||
− | + | Then we will add the values. | |
− | + | 1+2 is 3. | |
− | '''y | + | We print the value of '''y''' as 3. |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
− | | style="border-top: | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.035cm;padding-right:0.035cm;"| |
− | | style="border-top: | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| Like this, the conditions will be checked till the value of '''x''' will be less than or equal to 10. |
|- | |- | ||
− | | style="border-top: | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.035cm;padding-right:0.035cm;"| Highlight '''return 0;''' |
− | | style="border-top: | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| And this is our '''return statement'''. |
|- | |- | ||
− | | style="border-top: | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.035cm;padding-right:0.035cm;"| Highlight the '''semicolon''' |
− | | style="border-top: | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| Note that here, the '''while''' condition ends with a '''semicolon'''. |
|- | |- | ||
− | | style="border-top: | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.035cm;padding-right:0.035cm;"| |
− | | style="border-top: | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| In '''while''' loop, the condition does not ends with a '''semicolon'''. |
|- | |- | ||
− | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.035cm;padding-right:0.035cm;"| On the | + | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.035cm;padding-right:0.035cm;"| On the editor |
− | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| | + | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| NOW LET US SEE HOW TO EXECUTE THESE PROGRAMS IN C++ |
− | + | This is our while program in C++. | |
+ | |||
+ | The logic and implementation are same as in our C program. | ||
|- | |- | ||
− | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.035cm;padding-right:0.035cm;"| | + | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.035cm;padding-right:0.035cm;"| Point the cursor at '''while.cpp''' |
− | + | Highlight | |
− | + | '''<nowiki>#include<iostream></nowiki>''' | |
− | + | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| There are a few changes. | |
− | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| | + | |
− | ''' | + | Like the header file as '''iostream''' in place of '''stdio.h''' |
− | + | We have included the''' using''' statement here: | |
− | + | '''using namespace std;''' | |
− | |||
− | ''' | + | |- |
+ | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| And here we have used '''cout''' function in place of '''printf''' function. | ||
|- | |- | ||
− | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.035cm;padding-right:0.035cm;"| Highlight | + | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.035cm;padding-right:0.035cm;"| Highlight structure of '''while''' loop. |
− | ''' | + | '''<nowiki>while(x<=10)</nowiki>''' |
− | + | ||
− | ''' | + | '''{''' |
− | + | '''y+=x;''' | |
− | + | ||
− | + | ||
− | + | '''x++;''' | |
+ | |||
+ | '''}''' | ||
+ | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| The structure of '''while''' loop is same as in our '''C''' program. | ||
|- | |- | ||
− | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.035cm;padding-right:0.035cm;"| | + | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.035cm;padding-right:0.035cm;"| On the terminal. |
− | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| Let us | + | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"|Let us execute the program. |
+ | |||
+ | Come back to our '''terminal'''. | ||
+ | Let me clear the prompt. | ||
|- | |- | ||
− | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.035cm;padding-right:0.035cm;"| | + | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.035cm;padding-right:0.035cm;"| type: g++ space while.cpp space -o space while1 |
− | ''' | + | '''Press Enter''' |
− | ''' | + | type ./while1 |
+ | '''Press Enter''' | ||
− | |||
− | + | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| To execute type '''g++ space while.cpp space -o space while1''' | |
− | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| | + | |
+ | Press '''Enter'''. | ||
+ | |||
+ | Type '''./while1''' | ||
+ | |||
+ | Press '''Enter'''. | ||
+ | |||
+ | You can see that the output is similar to our '''while''' program in''' C'''. | ||
|- | |- | ||
− | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.035cm;padding-right:0.035cm;"| ''' | + | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.035cm;padding-right:0.035cm;"| Highlight the filename '''do….while''' loop |
+ | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| Now let us see the '''do….while''' program in '''C++''' | ||
+ | Come back to our text editor. | ||
− | ''' | + | |
− | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| Here | + | |- |
+ | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.035cm;padding-right:0.035cm;"| Highlight '''return 0;''' | ||
+ | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| Here also there are similar changes like the header file, the using statement and the cout function. | ||
|- | |- | ||
| style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.035cm;padding-right:0.035cm;"| Click on '''Save''' | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.035cm;padding-right:0.035cm;"| Click on '''Save''' | ||
− | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| | + | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"|Rest of the things are similar. |
+ | Let us execute the program. | ||
+ | |||
+ | |||
+ | Come back to our '''terminal'''. | ||
|- | |- | ||
− | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.035cm;padding-right:0.035cm;"| | + | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.035cm;padding-right:0.035cm;"| On the terminal |
− | ''' | + | type g++ space do-while.cpp space -o space do1 |
− | + | '''Press Enter''' | |
− | + | type | |
+ | ./do1 | ||
+ | '''Press Enter''' | ||
− | + | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"|Type '''g++ space do-while.cpp space -o space do1''' | |
− | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right: | + | |
− | + | ||
− | ''' | + | Press '''Enter''' |
− | + | Type '''./do1''' | |
− | + | ||
− | + | Press '''Enter'''. | |
− | We | + | We can see that the output is similar to our '''do-while''' program in '''C'''. |
|- | |- | ||
− | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.035cm;padding-right:0.035cm;"| | + | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.035cm;padding-right:0.035cm;"| Highlight |
− | + | ||
− | + | '''Output''' | |
+ | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| Now, we will see some common errors and their solutions. | ||
− | |||
− | + | |- | |
+ | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.035cm;padding-right:0.035cm;"| Switch to the editor | ||
+ | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| | ||
+ | |||
+ | Come back to our text editor. | ||
+ | |||
+ | |- | ||
+ | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.035cm;padding-right:0.035cm;"| '''int x=0;''' | ||
+ | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| Suppose here, I will not increment the value of '''x'''. | ||
− | + | Click on '''Save'''. | |
+ | Let us see what happens. | ||
− | + | Come back to our '''terminal'''. | |
|- | |- | ||
Line 493: | Line 520: | ||
'''Compile and execute''' | '''Compile and execute''' | ||
− | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| Let | + | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| Let me clear the prompt. |
+ | |||
+ | Let us execute the program. | ||
+ | |||
+ | Press the up arrow key twice. | ||
+ | |||
+ | Again press the up-arrow key. | ||
|- | |- | ||
Line 503: | Line 536: | ||
Highlight '''x undeclared''' | Highlight '''x undeclared''' | ||
− | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| | + | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| The output is displayed. |
+ | |||
+ | We can see a number of zeros. | ||
− | + | This is because the loop does not have a terminating condition. It is known as '''infinite loop'''. | |
+ | |||
+ | '''Infinite loop''' can cause the system to become unresponsive. | ||
+ | |||
+ | It causes the program to consume all the '''processors time'''. | ||
+ | |||
+ | But it can be terminated. | ||
|- | |- | ||
Line 515: | Line 556: | ||
at line no.6 | at line no.6 | ||
− | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| Come back to our program | + | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| Come back to our program. |
− | + | Let us fix the error. | |
− | + | Type '''x++''' and a '''semicolon'''. | |
− | + | ||
− | + | ||
Click on '''Save''' | Click on '''Save''' | ||
+ | |||
+ | Let us execute again. | ||
|- | |- | ||
Line 529: | Line 570: | ||
'''Compile and execute''' | '''Compile and execute''' | ||
− | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| | + | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| Come back to the '''terminal''' |
− | + | Press the up-arrow key. | |
+ | |||
+ | Yes, it is working. | ||
|- | |- | ||
| style="background-color:transparent;border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0cm;"| | | style="background-color:transparent;border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0cm;"| | ||
− | | style="background-color:transparent;border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0cm;"| | + | | style="background-color:transparent;border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0cm;"| This brings us to the end of this tutorial. |
− | + | We will move back to our slides. | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
Line 547: | Line 586: | ||
Summary | Summary | ||
− | | style="background-color:transparent;border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0cm;"| | + | | style="background-color:transparent;border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0cm;"|Let us summarize. |
− | + | In this tutorial we learned, | |
− | + | *'''while''' loop | |
− | + | **eg. <nowiki>while(x<=10)</nowiki> | |
− | + | *'''do….while''' loop | |
− | {….} | + | **eg. do (statement block) {….} (and while condition at the end) <nowiki>while(x<=10);</nowiki> |
− | + | ||
− | <nowiki>while(x<=10);</nowiki> | + | |
|- | |- | ||
Line 565: | Line 602: | ||
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0cm;"| As an assignment | | style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0cm;"| As an assignment | ||
− | Write a program to print the following using | + | Write a program to print the following using for loop |
0 1 2 3 4 5 6 7 8 9 | 0 1 2 3 4 5 6 7 8 9 | ||
− | '''Hint: '''the syntax of the for loop is | + | '''Hint: '''the syntax of the '''for''' loop is |
− | for ( | + | '''for (variable initialization; variable condition; variable increment\decrement) ''' |
− | + | (And here will be the body of the loop) | |
− | + | ||
− | body | + | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
'''{''' | '''{''' | ||
Line 599: | Line 617: | ||
'''}''' | '''}''' | ||
− | |||
|- | |- | ||
Line 605: | Line 622: | ||
About the Spoken Tutorial Project | About the Spoken Tutorial Project | ||
− | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| Watch the video available at the link shown | + | | style="border-top:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| Watch the video available at the link shown below |
It summarizes the Spoken Tutorial project | It summarizes the Spoken Tutorial project | ||
Line 637: | Line 654: | ||
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 | ||
− | More information on this Mission is available at: http://spoken-tutorial.org\NMEICT-Intro | + | More information on this Mission is available at the link shown below: http://spoken-tutorial.org\NMEICT-Intro |
|- | |- |
Latest revision as of 11:03, 31 January 2014
Title of script: Loops in C and C++
Author: Dhawal Goyal
Keywords: Loops, for loop, while loop, do....while loop, type casting, and Video tutorial
|
|
Slide 1 | Welcome to the spoken tutorial on Loops in C and C++ |
Slide 2
|
In this tutorial we will learn,
We will do this with the help of some examples. We will also see some common errors and their solutions. |
Slide 3
|
To record this tutorial, I am using
|
Slide 4 | Let us start with the introduction to loops.
Loops are used to execute a group of instructions repeatedly. |
Slide 5 | Depending on the purpose they are divided into three types:
|
Slide 6 | Let us start with the while loop first.
A while loop tests the condition in the beginning. The structure is- while ( condition ) { (within the brackets) statement block } |
Slide 7 | Now move on to the do….while loop
A do..while loop is executed at least once before the condition could be validated. The structure is- do { (within the brackets) statement block } (after the brackets) the while ( condition );
|
Now let us see an example on while and do...while loop.
I have already typed the code on the editor. Let me open it. | |
Point the cursor
while.c |
Note that our filename is while.c
Today we are going to learn addition of first 10 numbers using while loop. Let me explain the code now. |
Highlight
#include <stdio.h> |
This is our header file. |
Highlight
int main() { int x=0; int y=0; |
Inside the main function we have declared two integer variables x and y.
And initialized to 0. |
Highlight the while loop
while(x<=10) { y+=x; x++; } |
This is our while loop. |
Highlight while(x<=10) | The condition of the while loop is x is less than or equal to 10 |
Highlight y+=x;
printf( "%d\n", y ); |
Here the value of x is added to the value of y.
The value obtained after the addition is stored in y. Then we print the value of y. |
Highlight x++; | Here x is incremented.
That means the variable x is increased by one. |
Highlight
return 0; |
And this is our return statement. |
Now let us execute the program. | |
Open the terminal
Ctrl, Alt and T keys simultaneously |
Please open the terminal by pressing Ctrl, Alt and T keys simultaneously on your keyboard. |
Type
gcc while.c -o while Type ./while |
Type
gcc space while.c space -o space while Press Enter. Type ./while Press Enter. |
Highlight
Output 55 |
The output is displayed.
|
Now let us see the working of while loop.
Let me resize the windows. | |
Highlight the output. | Here 1st the value of x and y is 0. |
Highlight the output. | This is our while condition.
Here we check whether the value of x is less than or equal to 10. Which means the values of x will be from 0 to 10. Then we add y + x ie 0 + 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. 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 ie 0; + x ie 1. 0+1 is 1. We print the value as 1. Again x is incremented. 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 ie 1+2 which will give 3. We print the value as 3. Like this it will go on upto x is less than or equal to 10. |
Now, we will see the same program using do….while loop | |
Point the cursor to the filename. | Here is our program. Note that our fielname is do-while.c |
Highlight the part. | This part is already explained in the previous program.
So let us move on to our do-while loop. Here first the body of the loop will be executed and then the condition is checked. |
Highlight
y+=x; |
The value of x is added to the value of y
The value obtained after the addition is stored in y. |
The logic is same as in while program.
Now let us execute the program. | |
On the terminal. | Come back to our terminal.
|
Switch to terminal
Type: gcc space do-while.c space -o space do Type: ./do |
Type: gcc space do-while.c space -o space do
Press Enter. Type: ./do Press Enter. |
Highlight
Output |
We can see that the output is similar to our while program.
Now let us see the working of do-while loop. |
Let me resize the windows. | |
Highlight the output. | Here the value of x and y is 0.
We add those values. Then we will get 0. Now the value of y is 0 We print the value as 0. Then x is incremented by 1. Which means now the value of x is 1. Then the condition will be checked. You can see that the body of the loop is executed first. Anyhow if the condition is false, then also we will get a value ie 0. Now here we will check whether 1 is less than or equal to 10. The condition is true, again we will add the values. Now 0+1 Then we print the value of y as 1. |
Highlight the output. | Again, x will be incremented.
Now the value of x is 2. Then we check 2 is less than or equal to 10. We will go back here. Then we will add the values. 1+2 is 3. We print the value of y as 3. |
Like this, the conditions will be checked till the value of x will be less than or equal to 10. | |
Highlight return 0; | And this is our return statement. |
Highlight the semicolon | Note that here, the while condition ends with a semicolon. |
In while loop, the condition does not ends with a semicolon. | |
On the editor | NOW LET US SEE HOW TO EXECUTE THESE PROGRAMS IN C++
This is our while program in C++. The logic and implementation are same as in our C program. |
Point the cursor at while.cpp
Highlight #include<iostream> |
There are a few changes.
Like the header file as iostream in place of stdio.h We have included the using statement here: using namespace std;
|
And here we have used cout function in place of printf function. | |
Highlight structure of while loop.
while(x<=10) { y+=x; x++; } |
The structure of while loop is same as in our C program. |
On the terminal. | Let us execute the program.
Come back to our terminal. Let me clear the prompt. |
type: g++ space while.cpp space -o space while1
Press Enter type ./while1 Press Enter
|
To execute type g++ space while.cpp space -o space while1
Press Enter. Type ./while1 Press Enter. You can see that the output is similar to our while program in C. |
Highlight the filename do….while loop | Now let us see the do….while program in C++
Come back to our text editor.
|
Highlight return 0; | Here also there are similar changes like the header file, the using statement and the cout function. |
Click on Save | Rest of the things are similar.
Let us execute the program.
|
On the terminal
type g++ space do-while.cpp space -o space do1 Press Enter type ./do1 Press Enter |
Type g++ space do-while.cpp space -o space do1
Press Enter Type ./do1 Press Enter. We can see that the output is similar to our do-while program in C. |
Highlight
Output |
Now, we will see some common errors and their solutions.
|
Switch to the editor |
Come back to our text editor. |
int x=0; | Suppose here, I will not increment the value of x.
Click on Save. Let us see what happens. Come back to our terminal. |
Switch to the terminal
Compile and execute |
Let me clear the prompt.
Let us execute the program. Press the up arrow key twice. Again press the up-arrow key. |
Highlight error
Highlight Line no.8 Highlight loops1.cpp Highlight x undeclared |
The output is displayed.
We can see a number of zeros. This is because the loop does not have a terminating condition. It is known as infinite loop. Infinite loop can cause the system to become unresponsive. It causes the program to consume all the processors time. But it can be terminated. |
Erase int x=0;
from line no.10 and rewrite int x=0; at line no.6 |
Come back to our program.
Let us fix the error. Type x++ and a semicolon. Click on Save Let us execute again. |
Switch to the terminal
Compile and execute |
Come back to the terminal
Press the up-arrow key. Yes, it is working. |
This brings us to the end of this tutorial.
We will move back to our slides. | |
Slide 10
Summary |
Let us summarize.
In this tutorial we learned,
|
Slide 11 | As an assignment
Write a program to print the following using for loop 0 1 2 3 4 5 6 7 8 9 Hint: the syntax of the for loop is for (variable initialization; variable condition; variable increment\decrement) (And here will be the body of the loop) { body } |
Slide 12
About the Spoken Tutorial Project |
Watch the video available at the link shown below
It summarizes the Spoken Tutorial project If you do not have good bandwidth, you can download and watch it |
Slide 13
Spoken Tutorial Workshops
|
The Spoken Tutorial Project Team
Conducts workshops using spoken tutorials Gives certificates to those who pass an online test For more details, please write to, contact@spoken-tutorial.org |
Slide Number 14
|
Spoken Tutorial Project is a part of the Talk to a Teacher project
It is supported by the National Mission on Education through ICT, MHRD, Government of India More information on this Mission is available at the link shown below: http://spoken-tutorial.org\NMEICT-Intro |
This is Dhawal Goyal from IIT Bombay signing off
Thank You for joining. |