THE GOTO STATEMENT
THE GOTO
STATEMENT
C supports
the goto statement to branch unconditionally from one point to another
in the program. Although it may not be essential to use the goto
statement in a highly structured language like C, there may be occations
when the use of goto might be desirable. The goto requires a label in a
order to identify the place where the branch is to be made. A label is any
valid variable name, and must be followed by a colon. The label is placed
immediately before the statement where the control is to transferred. Before
goto statement. if condition must be added. The general forms of goto and label
statements are shown below:
Forward
jump
Backward
jump
if(test expression)
goto label;
label:
...............
statement;
...............
.............
...............
..............
label:
if(test
expression)
statement;
goto
label;
The label: can be anywhere
in the program either before or after the goto label; statement. During the
running of a program when a statement like goto begin; is met, the flow
of control will jump to the statement immediately following the label
begin:.Note that a goto breaks the normal sequential execution of the program.
If the label: is before the statement goto label; a loop will be formed and
some statements will be executed repeatedly. Such a jump is known as backward
jump. On the other hand, if the label: is placed after the goto label; some
statements will be skipped and the jump is known as a forward jump. A goto is
often used at the end of a program to direct the control to go to the
input statement, to read further data. Consider the following example:
main()
{
double x, y;
read:
scanf(“%lf”, &x);
if(x < 0)
goto read;
y = sqrt(x);
printf(“%lf\n”, y)
Comments
Post a Comment