5.15 – goto statement

by subbu on September 25, 2013

goto statement in C language:

It is the control flow statement rarely being used by the programmers in very special cases. Most of the times, it is discouraged from usage because by using goto, programs will become unreadable and unreliable. In some difficult situations it seems so simple to use goto to send the control wherever you want. However, it is recommended to use if, if-else or switch case to solve the problem rather using goto.

The purpose of goto is to send the control wherever you want within the program. Say for example if we want to send the control from one location to another location then we name the target location with a label and the label is specified with the goto keyword. So that, the goto sends the control to the specified location by skipping the statements in between.
Here the label must be a valid identifier and terminated with a colon (:)

goto statement in C language
Example:

#include<stdio.h>
int main()
{
 printf("One");
 printf("\nTwo");
 goto abc;               /* sends the control to abc: */
  printf("\nThree");
  printf("\nFour");
 abc:
 printf("\nFive");
 return 0;
}

Output:
Two
Five

Example explained:
The goto statement sends the control to the label “abc:” by skipping the statements in between. The statements printf(“\nThree”); and printf(“\nFour”); are not executed.

Example:

#include<stdio.h>
int main()
{
 if(10<20)
 {
   if(40!=50)
   {
     if(80>40)
       goto abc;
     printf("Hello");
   }
   printf("\nWorld");
 }
 abc:
 printf("\nEnd of program");
 return 0;
}

Output:
End of program

In this case, the goto is sending the control out of all the nested statements.

Example:


#include<stdio.h>
int main()
{
 fox:
   printf("CodingFox\t");
 goto fox;
 return 0;
}

Output:
CodingFox   CodingFox…

Example explained:
Here the goto statement is sending the control back to the 4th statement, results execution of printf() statement repeatedly. Executing a statement or block of statements repeatedly is called loop or iteration. Here the goto is generating an endless loop called unlimited loop or infinite loop.

Specification:
Print 10 to 15 natural numbers

#include<stdio.h>
int main()
{
 int n=10;
 abc:
   printf("\t%d",n);    /* printing n */
   n=n+1;                 /* incrementing by 1 */
 if(n<=15)
  goto abc;
return 0;
}

Output:
10      11      12      13      14      15

printing 10 to 15 natural number using goto

Example explained:
Here abc: and goto abc; forms a loop. The value of n is printed and incremented by 1 for every iteration as long as n is less than or equal to 15.

Specification:
Accept numbers one by one until zero is given and print the sum of them.

#include<stdio.h>
int main()
{
 int sum=0,n;
 printf("Enter numbers one by one and give 0 to stop:\n");
 first:
  scanf("%d",&n);
  if(n!=0)
  {
    sum=sum+n;
    goto first;
  }
 printf("Sum of all the numbers %d",sum);
 return 0;
}

Execution:
Enter numbers one by one and give 0 to stop:
34
56
78
0
Sum of all the numbers 168

adding numbers using goto

Example explained:
Here the label first: and goto first; forms a loop. For every iteration a new number is reading through scanf() and adding it to the sum. The control comes out of loop when n is 0.

goto can do impossible things!

Generally we can’t send the control into the conditional statement whose condition is false. But it can be done using the goto statement by violating the rules of control flow statements.

Example:

#include<stdio.h>
int main()
{
  goto abc;
  if(10>40)
  {
    abc:
     printf("Hello");
    goto pqr;
  }
 pqr:
 printf("\nWorld");
 return 0;
}

Output:
Hello
World

Example explained:
goto can do impossible things
In the above example goto could send the control into the “if selection statement” whose condition is false.

Previous post:

Next post: