goto statement
The goto statement is used for unconditional jump from one part of the program to another part of the program. It is always suggested not to use goto statement as this reduces the readability of the program. Use of goto statement is considered as poor programming approach.
Example
int main()
{
int cm;
printf(“Enter length in centimeters”);
scanf(“%d”,&cm);
if(cm<100)
goto label;
cm=cm%100;
printf(Wrong input is trimed”);
label:
printf(“cm=%d”, cm);
return(0);
}
Explanation:
- goto is a keyword that moves control to a location in the program mentioned by location name ‘label’.
- If value of cm is more than or equal to 100, it should be trimmed. For example if user enters 435, it becomes 35, 4 should be trimmed out.
- Try executing this program for different inputs.
More points about goto
- goto can move control in forward as well as backward in the program.
- goto works only within the same function body.
- Labels are names but no need to declare them as variables
goto statement