12.17.2011

goto statement

The goto statement is used to alter the normal sequence of program instructions by transferring the control to some other portion of the program.
The syntax is as follows:
goto label;
Here, label is an identifier that is used to label the statement to which control will be transferred. The targeted statement must be preceded by the unique label followed by colon.
label: statement;
It is recommended that as possible as skip the goto statement because when we use goto then we can never be sure how we got to a certain point in our code. They obscure the flow of control.

Note:- goto can never be used to jump into the loop from outside and it should be preferably used for forward jump.

Let us consider a program to illustrate goto and label statement:
/*program to demonstration of goto statement*/
#include<stdio.h>
#include<conio.h>
void main()
{
 int n;
 clrscr();
 printf("Enter one digit number: ");
 scanf("%d",&n);
 if(n<=6)
   goto mylabel;
 else
 {
   printf("Now control in main funcion.");
   exit();
 }
 mylabel:
   printf("Now control in mylabel.");
}

Output:-

Enter one digit number:9
Now control in main function.
Enter one digit number:4
Now control in mylabel. 

4 comments:

  1. make a program for reservation a seat in an airport...or just like booking a seat........

    ReplyDelete
  2. pls help me....

    ReplyDelete
  3. can the label be a constant??
    i.e
    if(x==1)
    goto 1;

    ReplyDelete
  4. But goto is not recommended by any of the programmsersU know y?

    ReplyDelete