GO TO Statement


GO TO Statement


GO TO Statement
This Keyword is used when we want to transfer the control to some point in the program which is not in the normal, step by step sequence of a program.

 It takes control from one place to another unconditionally, So a different  path for the execution of a program to set up.
The following program illustrates the use of a GOTO.

Example:63

        main()
        {
         printf("Hope is hoping against hope. . . . . . ");
         goto here;
         printf("Even it seems hopeless");
         hope();
         exit(0);
        }



OUTPUT:
     Hope is hoping against hope. . . . . .

Explanation:
     The second part of the message will not get printed,as due to goto, control skips  to the label hope and execution is terminated due to exit(0) present here.After hope, those would have got executed.

   Though goto seems to be handing over to us a magic word for placing control where are please, we would do well to empty combinations of IF, For,while and switch instead.This is because goto makes the program less readable and hard to debug. Besides,once the control is given to goto, there no telling how the program would behave, as is illustrated.

Example:64
         main()
        {
         int i;
         for(i=1;i<=5;i++)
         {
          if(i*i>=121)
          goto there;
          else
          printf("%d",i);
         }   
         there:
          printf("\n No more   murphys laws");
        }

OUTPUT:    1  2  3  4  5

Example:65

          main()
          {
           int i,j;
           for(j=1;j<=10;j++)
           {
            for(i=1;i<=10;i++)
            {
            if(j<10)
            goto out;
            printf("Murphy’s first law");
            }
            printf("If the price of the PC is a dream");
           }
           printf("Dream about a nightmare");
          }

OUTPUT:
        Dream about a nightmare

Explanation:Can we abruptly break out of the loop using goto?

    YES. In fact goto can take the  control anywhere in the program.Here, first time through  the loop itself the condition is satisfied,and goto takes the control directly to the label out, where the printf() gets executed.

Example:66

           main()
          {
           int i, k=1;
           here:
                if(k>2)
                 goto out;
               there:
               for(i=1;i<=5;i++)
               printf("%d",i);
               k++;
               goto there;
               out: ;
           }

OUTPUT:  12345 12345 12345 12345

Example:67

        main()
        {
         int i=1;
         switch(i)
         {
          case 1:
                 goto label;
          label:
          case 2:
                 printf(" He looks like a Saint. . . . . ");
                 break;
          }
          printf("\n Saint Bernard!");
        }

OUTPUT:He looks like a Saint. . . . .
       Saint Bernard!           .

Explanation:
       Since i is 1, case 1 is executed, which takes control  to label.Once  the control is here, contrary to what one might expect,case 2 gets executed,as is clear from the output. Legally speaking, case2 should have got executed only for value of i equal to 2. But goto overrules this,and whatever follows goto, it takes as the law.  

No comments:

Post a Comment