Google What is goto statement in C++ Language,its advantages and disadvantages with program example | Asp.Net,C#.Net,VB.Net,MVC,jQuery,JavaScipt,AJAX,WCF,Sql Server example

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

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

In this article and program i will explain what goto statement is and how to use it.  Goto statement is used to alter the normal sequence of program execution by transferring the 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;

Where 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.

Advantage:
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.

Disadvantages:
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 to understand.

#include<conio.h>
 #include<iostream.h>
 main()
 {
      int a,b;
      char ch;
      clrscr();
     y:cout<<"Enter any two numbers= ";
      cin>>a>>b;
      cout<<"First  Number= "<<a<<endl;
      cout<<"Second Number= "<<b<<endl;
      cout<<"Sum of " <<a<< " and "<<b<<" = "<<a+b<<endl;
      cout<<"Do you want more calculations ? Enter y for Yes and n for No= ";
      cin>>ch;
      if(ch=='y')
      {
                 goto y;
      }
      else if(ch=='n')
      {
                  goto n;     
      }
      else
      {
                  cout<<"Invalid choice...Enter y for Yes and n for No";          
      }
     n:cout<<"Please press enter to exit...HAVE A NICE DAY AHEAD!!!";
      getch();

   }  

Output:
Enter any two numbers= 40
60
First number= 40
Second number= 60
Sum of 40 and 60 = 100

Do you want more calculations? Enter y for Yes and n for No= y
Enter any two numbers= 12
13
First number= 12
Second number=13
Sum of 12 and 13 = 25

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."

No comments :

Post a Comment

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..