# Acceptable Use of the goto Statement?

So, I made a console interface. It prompts users with several options they can enter. 1 for the first option, 2 for the second option, etc. Using the switch statement, each option will bring them to a different screen. Now, after any of the options are entered and the users are brought to a particular screen, how do I get them back to the selection screen with those options? Would it be acceptable for me to do something along the lines of

selectionScreen:

printf("Enter 1 for the First Screen\n");
printf("Enter 2 for the Second Screen\n");
printf("Enter 3 for the Third Screen\n\n");

scanf("%d", &selection);

switch (selection)
{

case 1:

//do and print stuff here

for (i = 0 ; ; i ++)
{

//do and print stuff here

while(GetAsyncKeyState(VK_TAB))
{

goto selectionScreen;

}

printf("Enter the Tab key to go back the selection screen.\n\n");
printf("Enter any key besides Tab to reload!\n\n");

getchar();

system("CLS");

}

break;

case 2:

//do and print stuff here

for (i = 0; ; i++)
{

//do and print stuff here

while(GetAsyncKeyState(VK_TAB))
{

goto selectionScreen;

}

printf("Enter the Tab key to go back the selection screen.\n\n");
printf("Enter any key besides Tab to reload!\n\n");

getchar();

system("CLS");

}

break;

case 3:

//do and print stuff here

for (i = 0 ; ; i++)
{

//do and print stuff here

while(GetAsyncKeyState(VK_TAB))
{

goto selectionScreen;

}

printf("Enter the Tab key to go back the selection screen.\n\n");
printf("Enter any key besides Tab to reload!\n\n");

getchar();

system("CLS");

}

break;

default:

//do stuff here

}


Or is there a more favorable way of doing this

• Just slap a do while (true) around the whole thing and replace your gotos with breaks.
– 500 - Internal Server Error
Oct 2 '13 at 0:10
• It seems to be becoming more fashionable these days to justify using goto, and to recommend its use to beginners. IMO it's better to avoid it entirely, and on those rare occasions where it might actually be justified, just make your code very slightly more verbose than it needs to be. If you try to justify it, then I think you'll end up just justifying it too much and thinking every time is an exception, and I think it's best not to go down that road. Oct 2 '13 at 0:17
• I knew I'd be entertained by a goto question. What I find amusing is that goto generates so much ire in some people, and yet those same people often embrace exceptions, which are often little more than gotos with hidden destinations (not referring to anyone here). The goto has its place, which is usually bailing out of nested logic on error conditions (as in goto Failure;). But as a control flow mechanism? Bad idea in most cases. But bailing out to a single error handling point is often a perfect solution that simplifies and enhances maintainability.
– Carey Gregory
Oct 2 '13 at 1:23
• No argument, but no one's trying to justify the practice of unstructured gotos.
– Carey Gregory
Oct 2 '13 at 3:44
• @CareyGregory: You're welcome to your opinion, of course. If you think they "improve the clarity and maintainability of code", then it's your code, your rules. There's no "irrational religious holy war", though. It's just a bad programming practice, not a crime. Oct 3 '13 at 2:46

I'm unsure if the posted code is exactly what's intended functionally, but the following would do the equivalent:

do
{
printf("Enter 1 for the First Screen\n");
printf("Enter 2 for the Second Screen\n");
printf("Enter 3 for the Third Screen\n\n");

scanf("%d", &selection);

if ( selection < 1 || selection > 3 )
break;

switch (selection)
{
case 1:
//do and print stuff here
break;

case 2:
//do and print stuff here
break;

case 3:
//do and print stuff here
break;
}
}
while ( GetAsyncKeyState(VK_TAB) );


I really can't remember the last time I used a goto. I think it was in a FORTRAN class.

• @CareyGregory haha I wouldn't say that there's not some good uses for them. I'm sure you had a very good reason. And, OK, maybe I have, too, since the FORTRAN class. But I truly don't remember when it was exactly. :)
– mbratch
Oct 2 '13 at 1:34
• Thanks! My fault for being lazy when I wrote the example. Mostly because I was reading and writing from and to multi-level pointers. It wasn't representative of my actual code. Which is why I didn't get the exact answers I was looking for. I just edited it to what it should have been originally. However, what you wrote did really help me. I am now using an infinite do while loop and got rid of the goto 's. And thanks for the tip on the switch statement.
– chickeninabiskit
Oct 2 '13 at 4:03

Yes. It is acceptable.

You officially have permission to write code any way you want at any time. There are generally several ways to do just about anything. Your way works, and now you have valuable experience building control structures with gotos. That experience will be valuable in the future because other programs use gotos.

That being said, mbratch has put together another way to do it. I prefer his method, as it makes the code easier to read and maintain.

You should study and understand both techniques . Each technique is like an arrow. You put them in your quiver, and can head off to hunt bigger game. The more you know, the more dangerous you will be.

• Anyone can write whatever code they wish, but the point of Code Review is to help you write good code. Ever since Dijkstra's Go to statement considered harmful essay (ACM and widely reproduced elsewhere), programmers have accepted that gotos should be eliminated except where it is impractical to do so. Clearly this code easily can be restructured to eliminate the gotos, so most programmers would agree that a goto-free solution is superior. Oct 18 '13 at 20:05
• I would encourage you to read "go to statement considered harmful considered harmful" Dec 14 '13 at 13:26