The goto statement is used to alter normal sequence of program execution by transferring control to some other part of the program.In its general form the goto statement is written as
goto label;
where label is an identifier that is used to label the target statement to which control will be transferred.
All of the popular general purpose programming languages contain a goto statement, though modern programming practice discourages its use.
The following skeletal outline illustrates how the goto statement can be used to transfer control out of a loop if an unexpected condition arises.
/*main loop*/
scanf ("%f", &x);
while (x <=100) {
...................
if (x < 0) goto errorcheck;
...................
scanf ("%f", &x);
}
....................
/*error detection routine*/
errorcheck: {
printf("Error- Negative Value for x");
..................
}
In this example control is transferred out of the while loop, to the compound statement whose label is errorcheck, if a negative value is detected for the input variable x.
Monday, December 14, 2009
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment