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

From Script | Spoken-Tutorial
Jump to: navigation, search
(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…')
 
Line 28: Line 28:
 
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 45: Line 45:
 
|-
 
|-
 
| 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;"| Now let us start with the introduction to loops.
+
| 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 61: Line 61:
 
|-
 
|-
 
| 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 as follows
 
The structure is as follows
Line 69: Line 69:
 
'''while''' ( condition )  
 
'''while''' ( condition )  
  
{
+
{ (within the brackets)
  
 
statement block
 
statement block
Line 83: Line 83:
 
The structure is as follows
 
The structure is as follows
  
'''do''' {
+
'''do''' { (within the brackets)
  
 
statement block
 
statement block
  
} '''while''' ( condition )'''<nowiki>;</nowiki>'''
+
} (after the brackets)
 +
 
 +
'''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;"| Let us see an example on while and do...while loop in 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;"| 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.
  
So will just open it.
+
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
  
'''loops.c'''
+
'''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;"| Please note that I have saved the file with the name '''loops.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;"| Note that our filename is '''while.c'''
 +
 
 +
Today we are going to learn addition of first 10 numbers using while loop.
  
Today we are going to learn addition of first 10 numbers.
+
Let me explain the code now.
  
 
|-
 
|-
Line 115: Line 122:
  
 
'''int main()'''
 
'''int main()'''
| 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 '''main 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
+
  
 
'''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;"| The '''variable x and y are declared and initialized to 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;"| Inside the '''main '''function we have declared two integer variables x and y.
 +
 
 +
And initialized to 0.
 +
 
 +
 
  
|-
 
| 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;"| First we will look at the while loop.
 
  
 
|-
 
|-
Line 141: Line 147:
  
 
'''}'''
 
'''}'''
| 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 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;"| This is our while loop.
  
 
|-
 
|-
Line 149: Line 155:
 
|-
 
|-
 
| 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 the variable '''x''' is increased by one.
+
| 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.
| 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 commented '''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;"| I have commented the do-while loop here.
+
 
+
I will explain it after the 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
+
 
+
'''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 we print the value of y.
+
  
 
|-
 
|-
Line 173: Line 173:
  
 
'''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;"| This is our '''return statement.'''
+
| 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;"| 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;"| Now 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;"|  
 
| 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;"| Let us 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;"| Now let us execute the program.
  
 
|-
 
|-
Line 187: Line 183:
  
 
'''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;"| Open the terminal by pressing '''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;"| Please Open the terminal by pressing '''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: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 loops.c -o loop'''
+
'''gcc while.c -o while'''
  
To execute
+
To execute,
  
 
Type
 
Type
  
'''./loop'''
+
'''./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;"| To compile the program, type  
 
| 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 compile the program, type  
  
'''gcc loops.c -o loop'''
+
'''gcc while.c -o while'''
  
 
To execute,
 
To execute,
Line 207: Line 203:
 
Type
 
Type
  
'''./loop'''
+
'''./while'''
  
 
|-
 
|-
Line 213: Line 209:
  
 
'''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;"| Here the output is displayed as
+
| 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.
 +
 
  
'''55'''
 
  
|-
 
| 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 look at the same example with '''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;"| Switch to the editor
+
| 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:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| Let us again switch to '''loops.c'''
+
| 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: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;"| Comment the '''while loop''' and uncomment the '''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:0.05pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.035cm;"| Comment the '''while loop''' and uncomment the '''do….while loop'''
+
| 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: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: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;"| This is our while condition.
  
'''do'''
+
Here we check whether the value of x is less than or equal to 10.
  
'''{y+=x;'''
+
Which means the values of x will be from 0 to 10.
  
'''x++;}'''
+
Then we add y + x ie 0 + 0
  
'''<nowiki>while(x<=10);</nowiki>'''
+
We get 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;"| The structure of the do….while loop is different.
+
  
 +
We print the value of y
  
 +
Here we get 0.
  
 +
Then x is incremented.
  
|-
+
Which means now the value of x will be 1.
| 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;"| Highlight
+
  
'''y+=x;'''
+
Then we will check the condition again.
| 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;"| Here the value of '''x''' is added to the value of '''y'''
+
  
The value obtained after the addition is stored in '''y'''.
+
1 is less than or equal to 10
  
|-
+
If the condition is true then we will add the values.
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0cm;"| Highlight
+
  
'''x++;'''
+
Y ie 0 + x ie 1.
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0cm;"| Here the variable '''x '''is increased by one.
+
  
 +
0+1 is 1.
  
 +
We print the value as 1.
  
 +
Again x is incremented.
  
|-
+
Now the value of x is 2.
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0cm;"| '''<nowiki>Highlight while ( x <= 10 );</nowiki>'''
+
  
 +
We check the condition again.
  
 +
2 is less than or equal to 10
  
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0cm;"| Now we check the condition whether x 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="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0cm;"| 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;"|
 +
| 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;"|
 +
| 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;"|
 +
| 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.
  
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0cm;"| Now Click on '''Save.'''
+
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;"| Here the value of '''x''' is added to the value of '''y'''
 +
 +
The value obtained after the addition is stored in '''y'''.
 +
 +
|-
 +
| 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;"| 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;"|  
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0cm;"|  
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0cm;"| Switch back to 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 the terminal.
  
  
Line 288: Line 316:
 
| 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 '''
  
'''Compile and execute'''
+
Type:
| 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 compile and execute the program as before.
+
 
 +
'''gcc do-while.c -o do'''
 +
 
 +
Type:
 +
 
 +
'''./do'''
 +
| 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 do-while.c -o do'''
 +
 
 +
Type:
 +
 
 +
'''./do'''
  
 
|-
 
|-
| 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 55'''
+
| 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:1pt solid #000000;padding:0.035cm;"| Here the output is displayed as
+
  
'''55'''
+
'''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 the 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;"| Come back to our program.
+
| 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.
  
 
|-
 
|-
 
| 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;"| Here, we can see, in while loop the condition is checked first.
+
| 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.
  
If the condition is false, then the body of the loop will not be executed.
+
we add those values.
  
While condition does not ends with a semicolon.
+
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 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 will print the value of y as 1.
  
 
|-
 
|-
 
| 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;"| In do-while loop the condition is checked at the end.
+
| 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.
  
Here, if the condition is false,
+
Now the value of x will be 2.
  
Then also the body of the loop is executed once.
+
Then we check 2 is less than or equal to 10.
  
Here the body of the loop is executed once.
+
We will go back here.
  
Then the condition is checked.
+
Then we will add the values.
  
In do-while loop the while condition is ends with a semicolon.
+
1+2 is 3.
 +
 
 +
We print the value of y as 3.
 +
 
 +
|-
 +
| 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;"| Like this the conditions will be checked till the value of x will be less than or equal to 10.
 +
 
 +
|-
 +
| 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;"| And this is our return statement.
 +
 
 +
|-
 +
| 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;"| Note that here the while condition ends with a semicolon.
 +
 
 +
|-
 +
| 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;"| In While loop the condition does not ends with a semicolon.
  
 
|-
 
|-

Revision as of 16:12, 19 December 2013

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


Visual Cue
Narration
Slide 1 Welcome to the spoken tutorial on Loops in C and C++
Slide 2


In this tutorial we will learn,

for loop,

while loop and

do…while loop.

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

Ubuntu Operating System version 11.04

gcc and g++ Compiler version 4.6.1 on Ubuntu.

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:

while loop

do…..while loop

for loop

Slide 6 Let us start with the while loop first.

A while loop tests the condition in the beginning.

The structure is as follows

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 as follows

do { (within the brackets)

statement block

} (after the brackets)

while ( condition );


You can see that the condition is checked at the end.

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.
Type

gcc while.c -o while

To execute,

Type

./while

To compile the program, type

gcc while.c -o while

To execute,

Type

./while

Highlight

Output 55

The output is displayed.



Now let us see the working of while loop.

Let me resize the windows.

Here 1st the value of x and y is 0.
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
Here is our program. Note that our fielname is do-while.c
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;

Here 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.

Come back to the terminal.



Switch to terminal

Type:

gcc do-while.c -o do

Type:

./do

Type:

gcc do-while.c -o do

Type:

./do

Highlight

Output

We can see that the output is similar to the while program.

Now let us see the working of do-while loop.

Let me resize the windows.
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 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 will print the value of y as 1.

Again x will be incremented.

Now the value of x will be 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.
And this is our return statement.
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++

I have already made the program.

So lets take a look at it.

Point the cursor at loops1.cpp Please note that I have saved the file with the name loops1.cpp
Highlight

#include<iostream>

This is our header file iostream.
Highlight

using namespace std;

This is the using statement.
Highlight int main() This is the main function.
Highlight

int x=0;

int y=0;

Here the variables x and y are declared and initialized to 0.
Highlight structure of while loop

while(x<=10)

{

y+=x;

x++;

}

The structure of the while loop is the same for C++ as in C
Highlight the commented do….while loop The do….while loop is already commented
Highlight cout<<y<<”\n”; Here we print the value of y.
Highlight return 0; This is the return statement.
Click on Save Now click on Save.
On the terminal Let us compile

Come back to the terminal

Type

g++ loops1.cpp -o loop1

Type

./loop1

Type

g++ loops1.cpp -o loop1

press Enter

To execute

Type

./loop1

Highlight

Output

Here the output is displayed as

55

Switch to the editor Now let us see the do….while loop

Go back to the editor

Uncomment do….while loop and comment while loop Let us uncomment the do….while loop and comment the while loop
Highlight do….while loop

do

{y+=x;

x++;}

while(x<=10);

The structure of the while loop is the same for C++ as in C.
Highlight

cout<<y<<”\n”;

Here we print the value of y.
Click on Save Now Click on Save.
Switch to terminal

Compile and execute

Come back to the terminal

Let us compile and execute as before.

Highlight 55 Here the output is displayed as

55

Error 1 Now let us see some of the common errors which we can come across.

We switch to the editor and open loops1.cpp

int x=0; Now we look at the while loop.

Suppose we do not declare the variable x outside the while loop.

I will remove int x=0; from line no.6

And add it inside the while loop after line no.9

I will retain rest of the code as it is.

Save the program

Switch to the terminal

Compile and execute

Let us go to the terminal, compile and execute the program again.
Highlight error

Highlight Line no.8

Highlight loops1.cpp

Highlight x undeclared

We see that there is an error at line no.8 in our loops1.cpp file

Variable x undeclared.

Erase int x=0;

from line no.10

and rewrite int x=0;

at line no.6

Come back to our program

x should not be declared inside the loop.

Remove int x=0; from line no.10.

Rewrite it outside the loop at line no.6 as before

Click on Save

Switch to the terminal

Compile and execute

Let us go back to the terminal

Compile and execute the program.

Yes, it’s working.

Let us get back to our slides.

This brings us to the end of tutorial on loops.
Slide 10

Summary

In this tutorial we learned,

while loop

eg. while(x<=10)

do….while loop

eg. do

{….}

while(x<=10);

Slide 11 As an assignment

Write a program to print the following using all the loops

0 1 2 3 4 5 6 7 8 9

Hint: the syntax of the for loop is

for (var initialization; condition; var incre\decre)

{

body

}

Highlight/point the mouse on variable initialization; Here we initialize the variable
Highlight/point the mouse on condition; Here we write the condition for the loop
Highlight/point the mouse on variable update Here we update the variable
Highlight/point the mouse on

{

body

}

Here we write the body of the loop
Slide 12

About the Spoken Tutorial Project

Watch the video available at the link shown

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


Acknowledgement

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: http://spoken-tutorial.org\NMEICT-Intro

This is Dhawal Goyal from IIT Bombay signing off

Thank You for joining.

Contributors and Content Editors

Ashwini, Nancyvarkey