Sunday, 21 January 2018

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.  

Related Posts:

  • Introduction To ARRAYS Introduction To ARRAYS  An array is a group of related data items that share a common name.For instance,  we can define an array name Salary to represent a set of salaries of a group of employees. A particular … Read More
  • STANDARD LIBRARY FUNCTIONS STANDARD LIBRARY FUNCTIONS Arithmetic Functions: abs --> Returns the absolute value of an integer. cos --> Calculates cosine. cosh --> Calculates hyperbolic cosine. exp --> Raises the exponential e to the xt… Read More
  • Function Declaration and prototype Function Declaration and prototype It is one of the most important feature of ANSI C.A function prototype tells the compiler the type of data returned by the function, the number of arguments the function expects to r… Read More
  • Declaration of Arrays Declaration of Arrays  Like any other variables, arrays must be declared before they are used.The general form of array declaration is type variable-name [size]; The type specifies the type of elements that will be… Read More
  • Initialization of Arrays Initialization of Arrays We can initialize the elements of the array in the same way as the ordinary variables when they are declared.The general form of initialization of array is:  static  type  array-… Read More

0 comments:

Post a Comment