How do I do this without using goto statements?

Started by
44 comments, last by Unwise owl 2 15 years, 8 months ago
Quote:Original post by losethosLet's take a poll... anybody use the C/C++ "continue" statement? Screw that--keep my compiler lean.


What's wrong in making a code cleaner?

without continue:
while(someStuff){  // Do stuff 1 ...  if(someThing1)  {    // Do stuff 2 ...    if(someThing2)    {      // Do stuff 3 ...      if(someThing3)      {        // Do stuff 4...        // etc..      }    }  }}


with continue:
while(someStuff){  // Do stuff 1 ...  if(!someThing1) continue;  // Do stuff 2 ...  if(!someThing2) continue;  // Do stuff 3 ...  if(!someThing3) continue;  // Do stuff 4 ...  // etc..}

Advertisement
Quote:Original post by ManaStone
Here is a clicky to the diagram.


That looks interesting. It would look even better if your language had support for tail recursion:

let rec decision1 () =   if cond1 () then func2 () ; func3 () ; decision2 ()              else func7 ()and decision2 =  if cond2 () then func4 () ; func5 () ; decision3 ()              else func6 () ; func1 () ; decision1 ()and decision3 =   if cond3 () then func3 () ; decision2 ()              else func7 ()let run = func1 (); decision1 () 


With something as abstract as this, however, I would probably go for this automaton representation instead:

branch branches[] = {  { cond1, 6 }, // 0  { cond2, 5 }, // 1  { cond3, 6 }  // 2};step steps[] = {    { func1,  0,  1 }, // 0   { func2, -1,  2 }, // 1   { func3,  1,  3 }, // 2   { func4, -1,  4 }, // 3   { func5,  2,  2 }, // 4   { func6, -1,  0 }, // 5   { func7, -1, -1 }  // 6};// The required definitions: struct step {  boost::function0<void> execute;  int branch;  int next;};struct branch {  boost::function0<bool> condition,  int alternative;};int id = 0;while (id != -1){  int next_id = steps[id].next;  steps[id].execute();    if (steps[id].branch != -1)    if (branches[steps[id].branch].condition())      next_id = branches[steps[id].branch].alternative;  id = next_id;}

Quote:Original post by Telastyn
IMO

Give me an exception or flag or inner function any day. Goto/break/continue harm readability and the ability to maintain the code. Maintenance is hard enough without convoluting common flow control.


I think this code is more readable using continue. An if could have been used to surround the whole body of the loop, but that adds unneeded indentation.
The loop body is small so it's easy to see the continue at the top and keep in mind that the body is only being run for selected iterations:
	for( std::vector<SceneInfo>::iterator iScene = m_Scenes.begin();	     iScene != m_Scenes.end(); ++iScene )	{		if( !iScene->ptr )//ensure iScene is valid			continue;		//*snip* 7 lines of code that use iScene	}
In a perfect world though I wouldn't have invalid data in my vector to begin with ;)
It's rather unfortunate but many programmers out there that actually use goto think that people who don't ever use it actually hunt for ways to avoid it.

What they don't realise is that it is actually quite the opposite for programmers such as myself. It's only threads like this one that even remind me that the language has such a construct. I would struggle to find any use for it! I mean we're spoilt rotten with all kinds of constructs for modifiying control flow. Between the three kinds of loops, switch, break, continue, return, and not to mention exceptions, I can't find any reason to use yet another construct. I just don't have any use for it.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
The second func7 is just there, since its not clear if it is intended to be executed if the loop exits without the goto statement.

void mainloop(){	while(func1()==true)	{		func2();		while(func3()==true)		{			func4();			if(func5()==true)			{				func7();				return;			}					}		func6();	}func7();}
http://www.8ung.at/basiror/theironcross.html
I'd say, if it's generated code, the use of goto is perfectly valid, since there already is a higher level of abstraction to refer to and a human shouldn't be reading that output anyway.

This topic is closed to new replies.

Advertisement