C-and-C++/C3/Loops/English

From Script | Spoken-Tutorial
Revision as of 13:13, 3 May 2013 by Ashwini (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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 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 Now 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

A while loop tests the condition in the beginning

The structure is as follows

while ( condition )

{

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 {

statement block

} while ( condition );

Let us see an example on while and do...while loop in C.

I have already typed the code on the editor.

So will just open it.

Point the cursor

loops.c

Please note that I have saved the file with the name loops.c.

Today we are going to learn addition of first 10 numbers.

Highlight

#include <stdio.h>

This is our header file.
Highlight

int main()

This is our main function.
Highlight

int x=0;

int y=0;

The variable x and y are declared and initialized to 0.
First we will look at the while loop.
Highlight the while loop

while(x<=10)

{

y+=x;

x++;

}

This is the while loop.
Highlight while(x<=10) The condition of the while loop is x is less than or equal to 10.
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.

Highlight x++; Here the variable x is increased by one.
Highlight the commented do….while loop I have commented the do-while loop here.

I will explain it after the while loop.

Highlight

printf( "%d\n", y );

Here we print the value of y.
Highlight

return 0;

This is our return statement.
Click on Save Now Click on Save.
Let us execute.
Open the terminal

Ctrl, Alt and T keys simultaneously

Open the terminal by pressing Ctrl, Alt and T keys simultaneously.
Type

gcc loops.c -o loop

To execute

Type

./loop

To compile the program, type

gcc loops.c -o loop

To execute,

Type

./loop

Highlight

Output 55

Here the output is displayed as

55

Now, we will look at the same example with do….while loop
Switch to the editor Let us again switch to loops.c
Comment the while loop and uncomment the do….while loop Comment the while loop and uncomment the do….while loop
Highlight

do

{y+=x;

x++;}

while(x<=10);

The structure of the do….while loop is different.



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.

Highlight

x++;

Here the variable x is increased by one.



Highlight while ( x <= 10 );


Now we check the condition whether x is less than or equal to 10.
Click on Save


Now Click on Save.



Switch back to the terminal.



Switch to terminal

Compile and execute

Let us compile and execute the program as before.
Highlight 55 Here the output is displayed as

55

Come back to our program.
Here, we can see, in while loop the condition is checked first.

If the condition is false, then the body of the loop will not be executed.

While condition does not ends with a semicolon.

In do-while loop the condition is checked at the end.

Here, if the condition is false,

Then also the body of the loop is executed once.

Here the body of the loop is executed once.

Then the condition is checked.

In do-while loop the while condition is 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