IV. Go To Statement.: Goto Goto
IV. Go To Statement.: Goto Goto
Sc I Year / II Semester
UNIT – I ‘C’ Fundamentals, Tokens, Control and Decision Making Statements
IV. GO TO STATEMENT.
C has goto statement but one must ensure not to use too much of goto statement in their program
because its functionality is limited and it is only recommended as a last resort if structured
solutions are much more complicated.
The goto is a unconditional branching statement used to transfer control of the program from one
statement to another.
goto namel;
…………. ………….
…………. ………….
…………. ………….
name1:
Statement;
Here in goto,
name1 mentioned identifies the place where the branch is to be made.
name1 is a valid variable name followed by a colon.
name1 is placed immediately before the statement where the control is to be transformed.
A program may contain several goto statements. The names mentioned in goto must be unique
for branching.
Control can be transferred out of or within a compound statement and control can be transferred
to the beginning of a compound statement. However the control cannot be transferred into a
compound statement.
One must take care not to use too much of goto statements in their program or in other words use
it only when needed. This is because C being a highly structured language one must take care not
to use too much of these unconditional goto branching statements. The goto statement is
discouraged in C, because it alters the sequential flow of logic that is the characteristic of C
language. This word is redundant in C and encourages poor programming style.
#include <stdio.h>
#include <conio.h>
void main()
{
int a,b,c,d;
clrscr();
printf("Enter a and b value");
scanf("%d%d",&a,&b);
if(a>b)
goto pos;
else
goto neg;
pos:;
c=a-b;
printf("This the case where a is greater than b");
printf("\nThe a value is %d and b value is %d",a,b);
printf("\nThe subtracted result is %d",c);
goto out;
neg:;
d=b-a;
printf("This the case where b is greater than a");
printf("\nThe a value is %d and b value is %d",a,b);
printf("\nThe subtracted result is %d",d);
out:
getch();
}