In this tutorial you will learn about the C Go To Statement and its application with practical example.
C goto statement
The c goto statement is used to alter the normal execution of a program and transfer control to a labeled statement in the same program. We can have multiple goto and label statements in a c program. The goto statement is followed by a label name. The label is an identifier, which can be any plain text. It can be set anywhere in a c program. The label can be defined above or below to go to the statement. When a goto statement is encountered the compiler transfers the control to a label: specified with a goto statement. Then compiler begins execution from there.
GoTo Statement Syntax
Below is the general syntax of the goto statement in c programming:
Syntax:-
1 |
goto label; |
Program Structure:-
1 2 3 4 5 6 7 8 |
label1: ... ... goto label2; ... .. label2: ... |
goto Statement Example
Below is a simple example to demonstrate the use of the goto statement in c programming:
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <stdio.h> int main() { int age; printf("W3Adda - C goto Statement"); election: printf("\nEnter your age :"); scanf("%d", &age); if (age <= 17) { printf("\nYou are not eligible to vote."); goto election; } else { printf("You are eligible to vote."); } return 0; } |
Output:-