c goto statement
The goto statement is unconditional control statement that transfers the cursor of current to the another part of the program. goto statement can be used as—
goto;
label;
………
………
………
label:
statement
……..
……..
……..
|
label is any valid identifier and it is followed by semicolon. whenever the goto label is encounter then control is transfer to the define label, that is immediately after the label.
.
/* program to find the number is even or odd using goto statement*/
main()
{
num;
printf("Enter a number to check even or odd");
scanf("%d",&num);
(num%2==0)
{
even;
}
{
odd;
}
even:
printf("Number is even \n");
end;
odd:
printf("Number is ODD \n");
end;
end:
printf("\n");
0;
}
|
Enter a number to check even or odd 3
Number is ODD
|
we can be place label anywhere. if the label is after goto statement then the control is transferred forward and this is know forward jump and if the label is before goto statement then control is transfer to the backwards this is known as backward jumping.
note: The control can be transfer with in the function using goto statement not outside of function
When should we use goto?
when the condition in which using goto is preferable is when we need to break or jumps from the multiple loops using a single statement at the same time.
t main()
{
a, b, c;
(a=0;a<10;a++)
{
(b=0;b<5;b++)
{
(c=0;c<3;c++)
{
printf("\n",a,b,c);
(b == 3)
{
out;
}
}
}
}
out:
printf("out of the loops");
}
|
0 2 1
0 2 2
0 3 0
out of the loops
|