Open Source - programming foundamental - Asked By Najeeb Ullah on 22-Nov-11 07:36 PM

Short Answer with example.

Q1:- why it is prohibited to go to statement.
Riley K replied to Najeeb Ullah on 22-Nov-11 08:47 PM

Could you brief more what your question is??

Do you mean you want to know about go to stmnt

The goto statement is a control flow statement that causes the CPU to jump to another spot in the code. This spot is identified through use of a statement label. The following is an example of a goto statement and statement label:

#include <iostream>
#include <cmath>
int main()
  using namespace std;
tryAgain: // this is a statement label
  cout << "Enter a non-negative number";
  double dX;
  cin >> dX;
  if (dX < 0.0)
    goto tryAgain; // this is the goto statement
  cout << "The sqrt of " << dX << " is " << sqrt(dX) << endl;

In this program, the user is asked to enter a non-negative number. However, if a negative number is entered, the program utilizes a goto statement to jump back to the tryAgain label. The user is then asked again to enter a new number. In this way, we can continually ask the user for input until he or she enters something valid.

Jitendra Faye replied to Najeeb Ullah on 22-Nov-11 10:59 PM
Goto Statement

The goto statement simply moves the flow of control over to some label. e.g.

goto Hello

Goto will execute an unconditional jump to the destination. In order to place a condition on the jump it could be wrapped inside an if statement.

Disadvantages of Goto

Goto should not really be used in programs at all. They have a tendancy to make code unstructured and they make programs difficult to manage when they become significantly large.

It is much better practice to use the other flow control constructs and procedures.

you should avoid goto as much as you can.use only when using in large nested programs. otherwise use of goto makes the program unreliable,unreadable,and hard to debug.

one more big problem with goto is that when we do use them we can never be sure how we got to certain point in our code.they obscure the flow of control. so avoid them.

Suchit shah replied to Najeeb Ullah on 23-Nov-11 12:11 AM
The go to statement as it stands is just too primitive; it is too much an invitation to make a mess of one's program. One can regard and appreciate the clauses considered as bridling its use. I do not claim that the clauses mentioned are exhaustive in the sense that they will satisfy all needs
Although the use of goto is almost always bad programming practice (surely you can find a better way of doing XYZ), there are times when it really isn't a bad choice. Some might even argue that, when it is useful, it's the best choice.

Most of what I have to say about goto really only applies to C. If you're using C++, there's no sound reason to use goto in place of exceptions. In C, however, you don't have the power of an exception handling mechanism, so if you want to separate out error handling from the rest of your program logic, and you want to avoid rewriting clean up code multiple times throughout your code, then goto can be a good choice.
What do I mean? You might have some code that looks like this:

int big_function()
/* do some work */
/* clean up*/
return [error];
/* do some more work */
/* clean up*/
return [error];
/* do some more work */
/* clean up*/
return [error];
/* do some more work */
/* clean up*/
return [error];
/* clean up*/
return [success];

This is fine until you realize that you need to change your cleanup code. Then you have to go through and make 4 changes. Now, you might decide that you can just encapsulate all of the cleanup into a single function; that's not a bad idea. But it does mean that you'll need to be careful with pointers -- if you plan to free a pointer in your cleanup function, there's no way to set it to then point to NULL unless you pass in a pointer to a pointer. In a lot of cases, you won't be using that pointer again anyway, so that may not be a major concern. On the other hand, if you add in a new pointer, file handle, or other thing that needs cleanup, then you'll need to change your cleanup function again; and then you'll need to change the arguments to that function.

In some cases, this might be acceptable -- but if you're making a lot of changes to your code, adding in new variables, etc. -- it may not be worth the time or the extra lines of code to make that function call.

Instead, since you know that you're going to be executing only one piece of code and then returning from the function, you might as well use a goto to jump to the very end of the function, where you have your cleanup code.

Goto is a pretty simple keyword: you just need to include a "label" placed above the target location (followed by a colon), and then direct the program to go to the label. Note that this only works within the same function; you can't just enter one function from another.

goto label;
/* Code

goto is only being used to jump to a single point, it's not as though you're creating a mass of spaghetti code jumping back and forth in an attempt to simulate function calls. Rather, goto actually helps write more structured code.

There is one thing to be aware of: while your cleanup code should be able to free all of the memory you use, there may be times when you actually want to free that memory yourself and possibly reallocate it later. In these cases, if you do call free on a ptr and then have an if([error]) between that call to free and the subsequent call to malloc, you should definitely set the pointer to point to NULL! This will prevent your jumping to the cleanup code and then calling free on that pointer a second time, which can result in a security hole (the "double free" problem).

Goto should always be used sparingly, and as a last resort -- but there is a time and a place for it. The question should be not "do you have to use it" but "is it the best choice" to use it.
Reena Jain replied to Najeeb Ullah on 23-Nov-11 12:19 AM

A goto statement causes your program to unconditionally transfer control to the statement associated with the label specified on the goto statement.

A goto statement has the form:

Read syntax diagramSkip visual syntax diagram>>-goto--label_identifier--;-----------------------------------><

Because the goto statement can interfere with the normal sequence of processing, it makes a program more difficult to read and maintain. Often, a break statement, a continue statement, or a function call can eliminate the need for a goto statement.

If an active block is exited using a goto statement, any local variables are destroyed when control is transferred from that block.

You cannot use a goto statement to jump over initializations.

A goto statement is allowed to jump within the scope of a variable length array, but not past any declarations of objects with variably modified types.

Example of goto Statements

The following example shows a goto statement that is used to jump out of a nested loop. This function could be written without using a goto statement.

 ** This example shows a goto statement that is used to
 ** jump out of a loop.
#include <stdio.h>
#include <conio.h>
int main() {
  int n = 0;
  loop: ;
  printf("\n%d", n);
  if (n<10) {
  goto loop;
  return 0;
dipa ahuja replied to Najeeb Ullah on 23-Nov-11 01:58 AM
Why not GoTo Statement? 

You should avoid goto as much as you can. Use only when using in large nested programs. otherwise use of goto makes the program unreliable, unreadable, and hard to debug. One bigger problem with goto is that when we do use them we can never be sure how we got to certain point in our code. They obscure the flow of control. So avoid them.

Devil Scorpio replied to Najeeb Ullah on 23-Nov-11 02:56 PM

Fundamental of Programming

For most programming/scripting languages the concepts are all the same. The only thing that changes is the syntax in which it is written. Some languages may be easier to remember than others, but if you follow my guide here, it will make learning any programming language easier. This is in no way supposed to teach you everything about programming, just a general knowledge so when you do program you will understand what you are doing a little bit better.