Jump to content
  • Advertisement
Sign in to follow this  
ManaStone

How do I do this without using goto statements?

This topic is 3732 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Right now I am working on a program that generates pseudo code from an activity diagram. I am working out some diagram scenarios and trying to put it into code to help me come up with a plan to code the translation. There is one that I can’t figure out how to do without using a goto statement and I know that as a rule of thumb C++ programmers are not supposed to use those. This is the code that best describes the graph
	while(func1()==true)
	{
		func2();
		while(func3()==true)
		{
			func4();
			if(func5()==true)
			{
				goto g1;
			}
			
		}
		func6();
	}
g1:
	func7();


Also, if I were to copy everything after the outer while loop into the if statement and then return like in the code below, it would be way too impractical.
	while(func1()==true)
	{
		func2();
		while(func3()==true)
		{
			func4();
			if(func5()==true)
			{
				func7()
				return;
			}
			
		}
		func6();
	}

	func7();



Anyone know how I can do this? Is there a way I can use a double break or something? [Edited by - ManaStone on August 18, 2008 3:14:23 AM]

Share this post


Link to post
Share on other sites
Advertisement
Sometimes goto is not the ultimate evil. It's ultimate evil to some guys who spend all of their lives to write a book on how to write clean code.

In industry goto sometimes is way better than ugly hacks that are invented just to avoid it.

It's just like OOP, when it's too much, it's PITA.

Share this post


Link to post
Share on other sites
Just leave it. It's fine like that. goto is only bad if abused. It comes in handy in certain situations, hat's why it's still included in modern languages.

Share this post


Link to post
Share on other sites
Just use a flag


bool finished = false
while (!finished && func1())
{
func2();
while(!finished && func3())
{
func4();
finished = func5();
}
if (!finished)
{
func6();
}
}
func7();



It looks like more typing, but a properly named flag that describes the loop will help readability. I have faith that any decent compiler will optimise it to the same thing as well.

Share this post


Link to post
Share on other sites
You could use a flag:


bool done = false
while(!done && func1()==true)
{
func2();
while(!done && func3()==true)
{
func4();
if(func5()==true)
{
done = true;
}

}
if (!done)
func6();
}
func7();



It's likeley that there's a more elegant solution, depending on what funcs 1-6 actually do.

Share this post


Link to post
Share on other sites
Thanks for the advice. I'll probably end up using the goto though. Generating all the additional code for flags looks like it would be more tedious than it needs to be. I really wish C++ had the ultimate break that would get out of every loop. Perhaps I'll just have the pseudo code say Complete Break and then the programmer writes the code he can just add in the flags.

Share this post


Link to post
Share on other sites
Just use the goto. Yes, you could use some signal flag, but that would just make the code less intuitive. Give the label some descriptive name, add a comment what is done and it's quite comprehensible what happens.

The reason most programmers detest goto is that it can be used to make incomprehensible code. Using goto to break out of nested loops is fine. Using goto for any other flow control is frowned upon, especially if one jumps backwards in the program.

Share this post


Link to post
Share on other sites
Is the goto in your generated code, or your actual code? Gotos tend to be much more common (and more tolerated) in generated code since (in theory) no one ever has to maintain it directly, and can simplify the generating code.

Share this post


Link to post
Share on other sites
You could create another function, that will hold inner loop:


func8:
while(func3()==true)
{
func4();
if(func5()==true)
{
return true;
}
}
return false;

main:
while(func1()==true)
{
func2();
if (func8()) break;
func6();
}
func7();


Share this post


Link to post
Share on other sites
mceier does have an element of the solution. If your two inner loops somehow form a compact set of functionality that can be jumped out of, then that set of functionality should be given a name and placed in its own method. The actual details of how this should be happening obviously depend on the exact algorithm being used. Therefore:

void factored_method()
{
while(func1())
{
func2();
while(func3())
{
func4();
if(func5()) return;
}
func6();
}
}

void original_method()
{
factored_method();
func7();
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!