Goto Statement in C

goto transfers control directly to a labeled statement in the same function. It exists in C but should be used carefully because too much goto can make code hard to follow.

Syntax / Basic Pattern

goto label;
…
label:
statement;

 

Easy points to remember

  • The label ends with a colon.
  • goto can jump only within the same function.
  • For normal decisions and loops, if/else and loops are usually easier to understand.
⭐ PRACTICE TIP: Type these programs yourself. Change the values and run them again. That is how control statements become easy.

 

Program 1: Jump to label

#include <stdio.h>
int main(){goto show; printf(“Skipped”); show: printf(“Hello”); return 0;}

 

OUTPUT
Hello

 

Program 2: Goto exit

#include <stdio.h>
int main(){int n=-2;if(n<0)goto neg;printf(“Positive”);return 0;neg:printf(“Negative”);return 0;}

 

OUTPUT
Negative

 

Program 3: Goto even

#include <stdio.h>
int main(){int n=18;if(n%2==0)goto even;printf(“Odd”);return 0;even:printf(“Even”);return 0;}

 

OUTPUT
Even

 

Program 4: Goto retry label

#include <stdio.h>
int main(){int x=1;start: if(x<3){printf(“%d “,x);x++;goto start;}return 0;}

 

OUTPUT
1 2

 

Program 5: Goto end

#include <stdio.h>
int main(){for(int i=1;i<=3;i++){if(i==2)goto end;printf(“%d “,i);}end:printf(“Done”);return 0;}

 

OUTPUT
1 Done

 

Program 6: Goto positive

#include <stdio.h>
int main(){int n=25;if(n>0)goto yes;printf(“No”);return 0;yes:printf(“Yes”);return 0;}

 

OUTPUT
Yes

 

Program 7: Goto compare

#include <stdio.h>
int main(){int a=10,b=20;if(a<b)goto small;printf(“a is not smaller”);return 0;small:printf(“a is smaller”);return 0;}

 

OUTPUT
a is smaller

 

Program 8: Goto skip

#include <stdio.h>
int main(){printf(“A “);goto c;printf(“B “);c:printf(“C”);return 0;}

 

OUTPUT
A C

 

Program 9: Goto finish

#include <stdio.h>
int main(){int n=0;if(n==0)goto finish;printf(“Work”);finish:printf(“Finished”);return 0;}

 

OUTPUT
Finished

 

Program 10: Goto check

#include <stdio.h>
int main(){int marks=82;if(marks>=40)goto pass;printf(“Fail”);return 0;pass:printf(“Pass”);return 0;}

 

OUTPUT
Pass

 

Program 11: Goto vowel

#include <stdio.h>
int main(){char c=’a’;if(c==’a’||c==’e’||c==’i’||c==’o’||c==’u’)goto v;printf(“Consonant”);return 0;v:printf(“Vowel”);return 0;}

 

OUTPUT
Vowel

 

Program 12: Goto label sequence

#include <stdio.h>
int main(){goto second;first:printf(“First”);return 0;second:printf(“Second “);goto first;}

 

OUTPUT
Second First

 

Program 13: Goto with calculation

#include <stdio.h>
int main(){int a=5,b=6,sum=a+b;if(sum>10)goto big;printf(“Small”);return 0;big:printf(“Big”);return 0;}

 

OUTPUT
Big

 

Program 14: Goto error

#include <stdio.h>
int main(){int ok=0;if(!ok)goto error;printf(“OK”);return 0;error:printf(“Error”);return 0;}

 

OUTPUT
Error

 

Program 15: Goto menu-like

#include <stdio.h>
int main(){int c=2;if(c==1)goto add;if(c==2)goto view;goto end;add:printf(“Add”);goto end;view:printf(“View”);end:return 0;}

 

OUTPUT
View
Scroll to Top