So far, we have discussed if statements and how it is used in C programs to control the execution of statements based on certain decisions or conditions. The execution flow also depends on other statements that are not based on conditions that can control the flow.



C supports a unique form of a statement that is the goto Statement used to branch unconditionally within a program from one point to another. Although it is not a good habit to use the goto statement in C, there may be some situations where the use of the goto statement might be desirable. Programmers use the goto statement to change the sequence of execution of a C program by shifting the control to a different part of the same program.

The general form of the goto statement is:

Syntax:

goto label;

A label is an identifier required for the goto statement to be where the branch is to be placed. It is a valid variable name followed by a colon and put immediately before the statement where the control needs to be jumped/transferred unconditionally.

Syntax:

goto label;
        - - -- -   -
        - - - - - - - -
label:

statement - X;
/* This is the forward jump of the goto statement */

Or

label:
        - - -- -   -
        - - - - - - - -
goto label;

/* This is the backward jump of the goto statement */

An Example of a C Program to Demonstrate goto Statement

Example:

#include<stdio.h>

void main()
{
   int age;

   g: //label name
     printf("you are Eligible\n");
   s: //label name
     printf("you are not Eligible");

   printf("Enter you age:");
   scanf("%d", &age);
   if(age>=18)
        goto g; //goto label g
   else
        goto s; //goto label s
}

goto Statement in C - Video Tutorial

To understand "C goto Statement" in more depth, please watch this video tutorial.



Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram