Monday, 14 July 2014

Use of for,while and do-while Loop Control Structure in C programming

In programming you may have to do same operation more than one time. If we write same operation again and again then it will increases the size of your program. So we can Loop Control Structure. Using loop we can avoid this problem. It will repeating same portion of the program either a specified number of times or until particular condition is being satisfied.

There are three way to use loop control structure.
Using while Statement
Using for Statement
Using do-while Statement

While Statement

It is the simplest way to create a loop, the general form of “while” statement is
Initialize loop counter
while ( check loop counter using a condition )
{
do some operations;
loop counter;
}
This loop will continue until condition is true. The operations inside while loop will executed as many time the loop condition is true. In loop we use a loop counter by which we control the loop. Without this counter this program will executed infinitely.
Let’s write a program for “while” statement
#include<stdio.h>
void main()
{
    int i=1;
    while(i<10)
    {
        printf("%d ",i);
        i++;
    }
}
As we can see in this program that this loop will continue execution until value of variable “I” is less than 10. We use increment operator as loop counter here.
Let’s see the output for this program
1 2 3 4 5 6 7 8 9

For Statement

Create loop using for statement is most useful and maximum time used by programmer. It is most popular may be for simplicity. We can set three instruction in single line. They are “initialize counter”, “test counter” and “increment counter” can be written in single line. The general form of “for” statement is
for (initialize counter; test counter; increment counter)
{        
do some operations;
}
When we executing a “for” statement we fist initialize counter, then it will check the condition is true or not, then it will execute the operations inside it and lastly increase counter. But remember initialize counter instruction will run for the first time only. Then from the next stapes it will not initialize during loop.
Now let’s write a program for “for” statement
#include<stdio.h>
void main()
{
    int i;
    for(i=1;i<10;i++)
    {
        printf("%d ",i);
    }
}
It is the same operation we use for “while” statement. Now let’s check output
1 2 3 4 5 6 7 8 9


Do-While Statement

There is a little difference between “while” and “do-while” loop. First let see the general for this
do
{
 do some operations;
} while ( check loop counter using a condition )
The main thing is here first step of loop will executed without checking the condition.  So the operation inside the loop will be executed at least one time even when condition is fails.
Let’s write a program for “do-while” statement
#include<stdio.h>
void main()
{
    int i=1;
    do
    {
        printf("%d ",i);
        i++;
    } while(i<10);
}
You can see the difference between “while” and “do-while”. Now let’s see the output
1 2 3 4 5 6 7 8 9
You can use any one of this three in your program to create loop.

0 comments:

Post a Comment