Sunday, February 1, 2015

For loop and difference between goto and for loop

For loop and goto statement works in a same way. So actually there is no difference between them. Here will see the probe of this.

Input:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i;

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

    }
    return 0;
}



Output:

12345

Difference: no 

Input

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i;

    i=1;
    start:
        printf ("%d",i);
        i++;

        if (i<=5)
            goto start;

 ////   for (i=1; i<=5; i++)
 ////   {
////        printf ("%d",i);
////
 ////   }
    return 0;
}



Output:

12345

No comments:

Post a Comment