Is there a way to lable a section of code so if a choice is entered it will take the program to a certain peice of code?

Well I might but its kinda hard to explain what im doing. I am making more of a game thing. Could you just show me one easy way? Like can you use goto do make the choice to to a cetian peice of code? like if you have mian(lable) can you have goto main(lable); or something?

Never use goto statements - it's very bad programming practice! Why not just place your "certain piece of code" in a separate function and call it when the user makes a choice?

For example (using pseudocode - I haven't done C/C++ for a while):

If userChoice equals "Yes" then
      callYesFunction()
else
      callNoFunction()

Or you could have it all in one function and just pass the user choice in as a parameter:

callFunction(userChoice)

One often-used choice is a switch statement:

assuming 'command' is one char:

switch (command)
{
    case 'w':  // the following code is executed when (command == 'w')
        MoveFowardOneStep();
        break;  // break here means 'break out of the switch statement'

    case 'a':
    case 'A':  // here, the following code is executed for both 'a' and 'A'....
        MoveLeftOneStep();
        break;  // note that WITHOUT the break, the code keeps going on to the next case....

    // put more cases here

    default:  // default is invoked when none of the other cases match....
        GiveHelpMessage();
        break;
}

and the net effect is the same as a whole bunch of 'ifs'

yeah , why not u can take help of switch statement .....and in different cases either u can write particular piece of code or u can call upon different functions to do the same task

>Never use goto statements - it's very bad programming practice!
Moo, tell me, how does it feel to be cattle? I mean, you have to be a part of the herd if you mindlessly believe what you just stated.

*sigh* This argument is tiresome, so I'll leave you with this pearl of wisdom: goto isn't the problem, stupid programmers using goto is the problem.

>Never use goto statements - it's very bad programming practice!

There are occasions, especially in games, where a goto saves a function call or going through a bunch of selection statements. BTW when c++ is digested into assmebly all the program jumping (jmp statements) are gotos - as thats how assembly controls program flow, so every program uses goto in the end ;)

and instead of calling thousands of functions you could use a function pointer. This can help if the code in question is speed-critical and function pointers are often the way to eliminate a LOT of if statements and branching (sometimes hurts CPU pipelining)

There is a tutorial titled boolean algebra (it is really program logic) in the tutorials section which deals with this sort of thing :)

>Never use goto statements - it's very bad programming practice!
Moo, tell me, how does it feel to be cattle? I mean, you have to be a part of the herd if you mindlessly believe what you just stated.

*sigh* This argument is tiresome, so I'll leave you with this pearl of wisdom: goto isn't the problem, stupid programmers using goto is the problem.

Narue, in all of my programming work, I've never had the need to use a goto statement. To be honest, I was directed by the "farmer" (lecturer) in a large "herd" (lecture theatre) early on in my education to never touch a goto statement, so you could say I am mindlessly heeding the words of a single opinion. But the fact is that I agree with the lecturer - goto statements break up the structure and flow of a program and can make it very difficult for others to read it.

I have no use for it, and so I encourage others to find a better way of structuring their programs before resorting to a goto.

1) goto is mainly used by people with no clue as to how to avoid the need to use it.
2) people saying goto should never be used typically have no understanding of when and how it might be the most natural and/or efficient solution.
3) teachers will generally say to never use goto to prevent situation 1) but the end result is often that their students become the people meant in situation 2)

Is there a use for goto or similar constructs? Sometimes.
Is there enough of a use for them to advocate their use in general programming practice? Certainly not.

Java doesn't have goto (or rather it does have it as a reserved word but it isn't implemented and its use will cause a compiler error), maybe that's why Narue doesn't like the language ;)
I've myself never missed it (and I DID grow up using goto, seeing as my first programming language was IBM BASICA in the 1980s which had no other way to call different sections of code (named procedures didn't exist).
In fact I rarely (but not never) use the closest thing to it that Java does support, the labelled jump out of a loop.

I was a bit hasty in jumping on the anti-goto bandwagon to start with, especially without offering a decent explanation of why you shouldn't use it.

And I'll also admit I'm a big fan of Java. ;)

This article has been dead for over six months. Start a new discussion instead.