Sunday, December 18, 2011

Loops..... Oops...

The loops also known as iterations are used to execute a set of statements for many number of times until the condition is left unviolated.

The loops are implemented using 3 statements. They are

  1. for
  2. while
  3. do - while
for and while are entry level loops. i.e they check the condition at the beginning and only then iterates the statements.

do - while is an exit level loop. It first executes the statements and then checks the condition to decide whether to continue with the iteration or not.

1. for :
The syntax of the for loop is as follows :
The syntax of the for statement is as follows :
for(initialization; conditions; loop expressions)
{
    /* Statements to be executed */
}
Here, the initialization refers to initializing the loop variables required to performed the loop operations. It is done when the loop is executed for the first time. The conditions are the ones that decide whether the looping is to be done or not. And finally, loop expressions are the ones those are executed at the end of every loop. It is usually an increment/decrement operation.

Lets see an example to print your name 5 times :
#include<stdio.h>
void main()
{
    char name[] = "Your Name";
    int i;
    for(i=1;i<=5;i++)
    {
        printf("%s",name);
    }    
}
Output : Your Name Your Name Your Name Your Name Your Name
The for statement can also be written like this:
#include<stdio.h>
void main()
{
    char name[] = "Your Name";
    int i;
    i = 1;    /* Initialization of loop variable */
    for(;;)
    {
        if(i<=5)        /* Condition Satisfied*/            
            printf("\n%s",name);
        else            /* On Violation of Condition */
            break;      /* Exit from the loop */
        i++;            /* Loop Expression - Increment */
    }
}
2. while :
The while statement loops until the condition specified fails. Its syntax is given below :
while(condition)
{
    /* Statements to be executed */    
}
It will exit the loop when the condition fails.
#include<stdio.h>
void main()
{
    int i=1;
    printf("\n Demonstration of while loop \n\n");
    while(i<=5)
    {
        printf("\n Value of i : %d",i);
        i++;
    }
    printf("\n Exited from loop at i = %d",i);
}
Output : Demonstration of while loop Value of i : 1 Value of i : 2 Value of i : 3 Value of i : 4 Value of i : 5 Exited from loop at i = 6
3. do - while
Unlike the other two loop statements, do-while loop does not check the condition at the beginning, it does at the end and then decides whether to or not to continue with the iteration. Thus in this at least once, the loop will be executed. Also at the end of do-while the statement terminator ';' is used.
Syntax :
do
{
    /* Statements */
}while(condition);
Now, look at the following example :
#include<stdio.h>
void main()
{
    char option;
    printf("\n Demonstration of do-while loop");
    do
    {
        printf("\n Do you wish to continue with this loop ?(Press 'y' to continue) \t");
        scanf("\n%c",&option);    /* Enter the character */
    }while(option=='y'||option=='Y'); /* Check whether you entered 'y' or 'Y'. 
                                        If yes then continue with the loop. 
                                        Or else exit from the loop */
    printf("\nLoop has been exited since you entered %c ",option);
}
Thus in the do-while loop the condition is checked at the end of the iteration to decide whether to continue with the loop or break free from it.

No comments:

Post a Comment