C goto statement

Course- C >

The goto statement is known as jump statement in C language. It is used to unconditionally jump to other label. It transfers control to other parts of the program.

It is rarely used today because it makes program less readable and complex.

Syntax:

 
  1. goto label;  

goto example

Let's see a simple example to use goto statement in C language.

 
  1. #include <stdio.h>  
  2. #include <conio.h>  
  3. void main() {  
  4.   int age;  
  5.   clrscr();  
  6.    ineligible:  
  7.    printf("You are not eligible to vote!\n");  
  8.   
  9.    printf("Enter you age:\n");  
  10.    scanf("%d", &age);  
  11.    if(age<18)  
  12.         goto ineligible;  
  13.    else  
  14.         printf("You are eligible to vote!\n");  
  15.   
  16.    getch();  
  17. }  

Output:

You are not eligible to vote!
Enter you age:
11
You are not eligible to vote!
Enter you age:
44
You are eligible to vote!