Please disable your adblock and script blockers to view this page

What is goto statement in C language,its advantages and disadvantages with program example

Introduction: In this article i will explain with a program what is goto or we can say labelled statement, what are its advantages and disadvantages and how to calculate sum of two numbers and prompting whether to continue with more calculations or not using goto statement.

In this program i am going to explain what goto statement is and how to use it.  Goto statement is used to alter the normal sequence of program execution by transferring control of execution to some other part of the program unconditionally. So the control of flow transfers unconditionally to the statement specified by the label of goto statement.

Syntax for goto:
       goto label;
       statement 1;
       statement 2;
       label:statement n;

Here goto is a keyword and label can be any valid identifier.

In above syntax it is clear that after the execution of goto statement the control will jump unconditionally from there to statement-n which is prefixed by the label, without executing in between statements.

using goto statement  you can alter the normal sequence of the program execution so it gives the power to jump to any part of program.

It is always recommended not to use goto statement as this reduces the readability of the program. It becomes difficult to trace the control flow of a program, making the program logic complex to understand .Using goto statement is considered as poor programming practice. Any program written in C language can be written without the use of goto statement. So try to avoid goto statement as possible as you can.

There are two types of jump in goto statement:
      1. Forward jump
      2. Backward jump

In the example program below both the forward jump and backward jump is used.

Implementation: Let's create the program.

      int a,b;
      char ch;
      y:printf("Enter any two numbers= ");
      printf("First number= %d\n",a);
      printf("Second number= %d\n",b);
      printf("Sum of %d and %d  = %d\n", a,b,a+b);
      printf("\nDo you want more calculations? Enter y for Yes and n for No= ");
      scanf(" %c",&ch);
                 goto y;
      else if(ch=='n')
                  goto n;
                   printf("Invalid choice...enter y for Yes and n for No");
     n:printf("Please press enter to exit...HAVE A NICE DAY AHEAD!!!");

run the program using ctrl+F9

Enter any two numbers= 11
First number= 11
Second number= 22
Sum of 11 and 22 = 33

Do you want more calculations? Enter y for Yes and n for No= y
Enter any two numbers= 56
First number= 56
Second number=88
Sum of 56 and 88 = 144

Do you want more calculations? Enter y for Yes and n for No= n
Please press enter to exit...HAVE A NICE DAY AHEAD!!!");

 Now over to you:
"If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linked in and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned for more technical updates."

Next Post »

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily.. ConversionConversion EmoticonEmoticon