goto Statement in C

goto Statement in C :

The goto statement in C or C++ is rarely used in programming today. This statement is a part of jump statements in C or C++.

What does a goto Statement do ?

  • A goto statement in C or C++ can transfer the program control anywhere in the program i.e. alters the normal sequence of program execution by transferring control to some other part of the program.
  • The target definition of a goto statement is marked by a label.
  • The target label and goto must appear in the same function.

Syntax of goto statement in C or C++ is :

goto label;
      :
label :

Where label is a user supplied identifier and can appear before or after goto.

Example of goto statement in C :

 :
x=0;
top :
printf("x = %d \n",++x);
if(x < 10) 
   goto top;
 :

The above example prints the numbers from 1 to 10. The printf or cout prints the value of ++x. If x is less than 10, the control is transferred to the label top. Otherwise, the control is transferred after the if block.

Flow Diagram of goto Statement :

goto Statement in C

Some Points regarding goto Statement :

Point 1 : If a label appears just before a closing brace, then the solution is to place a null statement following the label.
A closing brace may not come immediately just after a label. For example, consider the following code :

    :
{
   goto bottom:
    :
   bottom :       /*wrong  ! Because the label is preceding the
                    closing brace. */
}

The above code is wrong due to a closing brace coming immediately just after a label.

Solution : To handle it, a null statement may be used that follows the label as shown below :

    :
{
   goto bottom:
    :
   bottom : ;        //correct now ! !
}
Point 2 : goto statement may not jump forward over a variable definition. For example, the following code is wrong :

main()
{
     goto last;         //jumping over a variable definition
     char ch = 'a';
        :
     last :
          :
}

Solution : The forward jumping over a variable definition is possible only if the variable definition is occurring in a block and the entire block is jump over, such as shown below :

     :
main()
{
    goto last;   //correct now.
    :
    { char ch = 'a';
        :
    }
    last :
     :
}
Point 3 : The reverse of the above constraint i.e. a backward jump over and initialized variable is allowed in c++.

Why goto statement considered harmful ?

  • Use of goto statement makes a program difficult to understand and debug because the control of the program won’t be easy to trace and it makes program confusing, less readable and complex.
  • Goto statement is considered a harmful construct and a bad programming practice.
  • [2]goto statement does not limit scope
    • hence any temp variables used inside, won’t be freed until later.
    • hence the same variable names cannot be re-use later in future code in the same scope.
    • hence it could lead to bugs.
    • hence you have the chance of skipping over a variable declaration.

Solution :

It can be replaced in most of C programs with the use of break and continue statements. All programmer should try to avoid go to statement as possible as they can.

A Simple Program on goto Statement :

/* A C program to illustrate the working of goto statement. */
/* This program tell whether a person is eligible for marriage or not */
/* If person's age is equal or more than 18, person is eligible for the marriage 
   Otherwie the person is not eligible*/

int main()
{
   int age;
   Marriage:
   printf("The person is not eligible for marriage");

   NoMarriage:
   printf("The person is not eligible to marriage");

   printf("Enter a person's age:");
   scanf("%d", &age);
   if(age>=18)
        goto Marriage;
   else
        goto NoMarriage;
   return 0;
}

Reference [2] : http://stackoverflow.com/questions/245742/examples-of-good-gotos-in-c-or-c

     

Incoming search terms:

  • use of ON-GOTO statement in basic use diagram

Leave a Reply