Saturday, December 17, 2011

LOOP

Introduction

I will always do my home work !
When there is a provision of printing it 100 times with the help of a program.
Either we can do it in the following way :

#include"stdio.h"
main()
{
    printf("I will always do my home work ! \n");
    printf("I will always do my home work ! \n");
    printf("I will always do my home work ! \n");
    /*...................
    .....................
    .....................
    till 100 times */
}

Or do it in this way :

#include"stdio.h"
main()
{
    int i=1;

    while(i<=100)
    {
        printf("I will always do my home work ! \n");
    i++;
    }
}

Loop is used to repeat a single or a set of statements several times in a program. Statement inside {} of  any loop will remain executed until the condition gets false.There are three kinds of Loops, which are :
  • while
  • for
  • do while 

While Loop

Syntax for while is :

    while(condition)
    {
        Statement 1;
        Statement 2;

    increment;
    }


For example :

#include"stdio.h"
main()
{
    int counter, i=1;

    printf("Enter a Number : ");
    scanf("%d",&counter);

        while(i<=counter)
        {
            printf("%d I Wanted to Repeat This Statement %d times \n",i,counter);

        i++;
        }

    printf("Out from the loop ! \n");
    printf("Now the statements here will be executed once ! \n");
    printf("Because we are out from the while loop ! \n");
}

If we have to exit from a loop on a certain condition then we have to use a keyword called break
For example :

#include"stdio.h"
main()
{
    int i=1;

    printf("The loop will run till 100 ! \n");

        while(i<=100)
        {
            printf("i = %d I am inside the loop but will exit when value of i becomes 50 \n",i);

            if(i==50)
            {
                printf("Good Bye Friends ! \n");
                break;  // Exit from the loop
            }

        i++;
        }

    printf("Out from the loop ! \n");
}

Opposite of break is continue which is also a keyword.

For Loop

Function of for is also same as while but the syntax is different as shown below :

    for ( initialization ; condition ; increment )
        {
                statement 1 ;
                statement 2 ;
        }

Lets construct the previously made programs with the help of for.

#include"stdio.h"
main()
{
    int counter, i;

    printf("Enter a Number : ");
    scanf("%d",&counter);

        for(i=1;i<=counter;i++)
        {
            printf("%d I Wanted to Repeat This Statement %d times \n",i,counter);
        }

    printf("Out from the loop ! \n");
    printf("Now the statements here will be executed once ! \n");
    printf("Because we are out from the for loop ! \n");
}

#include"stdio.h"
main()
{
    int i;

    printf("The loop will run till 100 ! \n");

        for(i=1;i<=100;i++)
        {
            printf("i = %d I am inside the loop but will exit when value of i becomes 50 \n",i);

            if(i==50)
            {
                printf("Good Bye Friends ! \n");
                break;  // Exit from the loop
            }
        }

    printf("Out from the loop ! \n");
}

Do While Loop

Like in while and for, conditions are checked at the starting point of the loop but here it is checked at the ending point of the loop. That will make one difference that is for at least once the statements inside the loop will be executed and then the condition will be checked. Syntax for do while is :

    do
    {
        Statement 1;
        Statement 2;

    }while(condition);

For example :

Addition of Two Numbers :

#include"stdio.h"
main()
{
    int a,b;
    char c='y';

    do
    {
        printf("Enter First Number : ");
        scanf("%d",&a);
        printf("Enter Second Number : ");
        scanf("%d",&b);
        printf("Addition of %d and %d is %d \n",a,b,a+b);

        printf("Want to continue(y/n) : ");
        fflush(stdin);
        scanf("%c",&c);

    }while(c=='y');
}

Here the program will not be terminated until we press any other key except "y". This kind of loop is also called as Odd Loop.

Odd Loop

In some programs we have to decide when to get exit from the loop during the run time, for this odd loops are created.
For example :

#include"stdio.h"
main()
{
    int i,f=0;

    do
    {

    printf("To Exit from this Loop a Negative Number has to be Entered : ");
    scanf("%d",&i);

        if(i>=1)
            f=f+i;

    }while(i>=0);

printf("Sum of all Positive Numbers You Entered is %d \n",f);
}

It can be done with for and while as well.

Nested Loop

There can be a loop inside a loop, just be careful with the braces {}, sometimes it gets confusing in case of a complicated program.
Syntax for Nested While :

while(condition)
{

    while(condition)
    {
        Statement 1;
        Statement 2;
    }

Statement 1;
Statement 2;
}

Syntax for Nested For :

for(condition)
{

    for(condition)
    {
        Statement 1;
        Statement 2;
    }

Statement 1;
Statement 2;
}

Or all kinds of loops can be nested as well.

For example :

Multiplication of 1 to 20 :

#include"stdio.h"
main()
{
    int cal, i, j=1, a=1;

    while(a<=20)
    {
        i=1;
        j=a;

        for(i=1;i<=10;i++)
        {
            cal=j * i;
            printf("%d * %d = %d \n",j,i,cal);

         }  //   End of For

    printf("\n");
    a++;

    }  //  End of While

    printf("Done \n");

}  //  End of Main

Lets add do while to it :

#include"stdio.h"
main()
{
    char ch ='y';

    do
    {
        int cal, i, j=1, a=1;

        while(a<=20)
        {
            i=1;
            j=a;

            for(i=1;i<=10;i++)
            {
                cal=j * i;

                printf("%d * %d = %d \n",j,i,cal);

            }

        printf("\n");
        a++;

         }

    printf("Want to run this again (y/n) : ");
    fflush(stdin);
    scanf("%c",&ch);

    }while(ch=='y');  //   End of do while

    printf("Finally Done \n");
}

Infinite Loop

When the condition is not set or the variable is not initialized properly, the loop does not gets terminated and keeps on executing the statements under it.
For example : (No need to run this two programs below)

#include"stdio.h"
main()
{
    int i;

        for(i=1;  ;i++)
        {
           printf("It will keep on Run ! \n");
            printf("Cant Stop It \n");
        }
}

#include"stdio.h"
main()
{
        for(int i;i<=2;i++)
        {
            printf("It will keep on Run ! \n");
            printf("Cant Stop It \n");
        }
}

Few More Examples

Addition of First 10 Numbers :

#include"stdio.h"
main()
{
    int i,sum=0;

        for(i=1;i<=10;i++)
        {
            sum=sum+i;
        }

    printf("Addition of First 10 Numbers is %d \n",sum);
}

First 10 Even Numbers :

#include"stdio.h"
main()
{
    int i,sum=0;

    printf("First 10 Even Numbers are :");

    for(i=2;i<=10;i=i+2)
        {
            printf(" %d, ",i);
        sum=sum+i;
        }

    printf("\nAddition of First 10 Even Numbers is %d \n",sum);
}

Display 10 to 1 :

#include"stdio.h"
main()
{
    int i;

    printf("Display 10 to 1 :");
       
    for(i=10;i>=1;i--)
        {
            printf(" %d, ",i);
        }
     printf("\n");
}

Reversed Number :

#include"stdio.h"
main()
{
    int i,f=0;

    printf("Enter a Number : ");
    scanf("%d",&i);

    while(i>0)
    {
        f=f*10+i%10;
        i=i/10;
    }

    printf("Reversed Number is %d \n",f);
}

Print All Prime Numbers :

#include"stdio.h"
main()
{
    int n,i=2,j;

    printf("Enter a Number : ");
    scanf("%d",&n);

    for(j=1;j<=n;j++)
    {
        i=2;
        while(i<j)
        {
            if(j%i==0)
            break;
        i++;
        }
            if(i==j)
            printf("%d, ",j);
    }
    printf("\n");
}

Factorial of a Number :

#include"stdio.h"
main()
{
    int i,f=1;

    printf("Enter a Number : ");
    scanf("%d",&i);

    while(i>=1)
    {
        f=f*i;
    i=i-1;
    }

    printf("Factorial is %d \n",f);
}

Print 1 to 15 in a Different Way :

#include"stdio.h"
main()
{
    int i,j;

    for(i=1;i<=15;i++)
    {
        for(j=1;j<i;j++)
            printf("%d",j);

    printf("\n");
    }
}

Print ASCII :

#include"stdio.h"
main()
{
    int i;

    printf("Print ASCII : ");

    for(i=0;i<=255;i++)
        printf("\n  %d = %c ",i,i);

    printf("\n");
}

No comments:

Post a Comment

Followers