How do I do this without using goto statements?

Started by
44 comments, last by Unwise owl 2 15 years, 7 months ago
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]
-----------------------------Download my real time 3D RPG.
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.
______________________________Madman
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.
Just use a flag

bool finished = falsewhile (!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.
You could use a flag:

bool done = falsewhile(!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.
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.
-----------------------------Download my real time 3D RPG.
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.
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.
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();

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();}

This topic is closed to new replies.

Advertisement