1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Tip for beginners while using goto

Discussion in 'C' started by PRADIP, Mar 22, 2007.

  1. PRADIP

    PRADIP New Member

    The goto statement in C is used to transfer control to desired place in program but it is recommeded that goto statement should be avoided as it creates an unexpecting results. Let us see how this occurs.

    The syntax of goto statement is: goto label; Where label is a word representing statement or block of statements.
    e.g.
    output: printf("this is the output"); Here output is a lebel to printf() statement.

    when the statement goto output is executed, the control is taken to the printf() statement having label output in the program.

    Now lets consider following program
    Code:
     #include<stdio.h>
     #include<conio.h>
     
     void main()
     {
      int n;
      clrscr();
    
      printf("Enter the number");
      scanf("%d",&n);
      if(n%2==0)
        goto even;
      else 
        goto odd;
      
      even: printf("\nThe number is even");
      odd: printf("\nThe number is odd");
      
      getch();
     }
    While executing the program, if you enter a odd number then the control will be passed to 'odd' lebel and the output would be "The number is odd" which is correct. But if you enter a even number then the output would be:
    "The number is even"
    "The nuber is odd"
    Which is certainly not expected. This occurs becuase goto just pass control to the given label and doesnot check any condition or case. Therefore it execute all statement following labeled statement. To avoid such a problem, simply use one another lebel say 'end'and use it as follow.

    Code:
     #include<stdio.h>
     #include<conio.h>
     
     void main()
     {
      int n;
      clrscr();
    
      printf("Enter the number");
      scanf("%d",&n);
      if(n%2==0)
        goto even;
      else 
        goto odd;
      
      even: printf("\nThe number is even");goto end;
      odd: printf("\nThe number is odd");goto end;
      
      end:getch();
     }
    ] Now you can use goto statement anywhere in program and avoid unexpected results using this trick.
     
  2. DaWei

    DaWei New Member

    First, please learn to post code inside code tags so that the indentation and formatting is preserved.

    Second, the use of goto, while not "evil", is contraindicated simply because it leads to spaghetti code when improperly used. I believe that it is better to recommend against its usage, than to show how to overcome its deficiencies. The use of goto as you illustrate it is merely an indication that the coder has failed to understand the proper use of if and loop constructs. It is better not to support this ignorance.

    When a user has gained experience in convoluted programs, he or she will recognize that very rare instance in which a goto provides the most effective solution.

    The essential part of your code could be replace with
    Code:
    if (n%2) puts "\nThe number is odd"; else puts "\nThe number is even";
    
    There are other issues with your code. Scanf should not be used without testing it for success, and particularly when one is seeking a number, which requires conversion as well as input. I can break your code simply by entering "Z". This is not robust. Read the documentation for the functions you use. Specifically pay attention to the indications provided for failures.

    Another issue is the use of things like conio.h, which destroy portability. Use them only if the feature is truly important and portability is of no concern.
     
  3. shabbir

    shabbir Administrator Staff Member

    As you say go to can result into an error and you are trying to solve the error using some more goto's Contradictions all round. Dont take this personal.
     

Share This Page