I'm keeping this abstract and language-ambiguous because I'm looking for information about the
philosophy behind it, not necessarily the exact code to write. I want to implement the following logic:
For items 1 to
n, perform a logical test on each item. If all tests are False, Path A is chosen. As soon as any test returns True, Path B is chosen.
Assuming the language has the standard set of DO, FOR, WHILE, EXIT, etc. statements, I'd like to identify the most elegant way to map this logic. I've come up with the following (pseudocode ahoy):
FOR x = 1 to 10
IF x.flavor = "cherry" THEN GOTO path_b
NEXT x
path_a:
...
END
...
path_b:
...
As the saying goes, I know only enough about programming to be dangerous, so I know that using GOTO-type functions is generally inefficient and frowned upon where it's avoidable, though I don't know how to avoid it in this case. Is there a better solution that doesn't involve GOTO or excessive IF/THEN statements, and that only uses the most elementary of functions? This strikes me as a basic problem and I'm guessing there's a simple answer that will be obvious once someone points it out to me.
Thanks for any help.
Posts
You're pretty much there but you want to through in some hot subroutine action:
I made a game, it has penguins in it. It's pay what you like on Gumroad.
Currently Ebaying Nothing at all but I might do in the future.
1) put path a and b inside your for loop and follow appropriately.
2) create a variable and if the test returns true, set your variable to true value and exit the for loop. Then outside of the loop check the value of that variable to determine which you need to execute.
int x = 1;
while(flag && (x++ < 11))
{
/* If test fails, set flag to 0 and quit loop */
if (TEST RETURNS FALSE)
flag = 0;
}
/* All tests succeeded */
if (flag)
pathA;
else pathB;
Sorry after typing all this I remembered your philosophy statement and not asking for exact code. It's already up there but there's nothing that's rocket science in what I put.
PSN: TheScrublet
If you're a golfer, perhaps:
Or depending on how sophisticated your language lets your for loop be:
This of course assumes a more traditional language. If you were trying to be Pythonic or functional about it, you would probably define or find an operation that takes an iterator (or iterable) and a test, and then returns true if any of the items in that iterable pass the test (with short circuiting, of course).
PSN: TheScrublet
In this case, the small requirement of extra space on the stack for the test boolean outweighs the readability loss due to the goto.
edit: oops, beated.
Usually a goto is NOT advised.. as you generally want to exit the loop for the exception, and return to continue processing. Unless you want to exit the loop entirely.. using your example you want to try it 10 times.. and when a condition is found.. stop processing the loop.
The variable question is also language dependent too. In the example you posted in the OP, the goto label is still theoretically taking up memory space.. much the same way a variable would. But the ease of checking some variable against true / false is much better then the cost of the variable involved.. especially if you can reuse or garbage collect it later. Your example would leave the loop counter still in memory as you are technically still in the loop when you goto out of it.. unless you are using basic or something..
Also in your example above... is the counting to 1-10 important? or just the loop until 'cherry' is found? A do..while loop is better for that purpose... especially for expansion of code later on.
Librarians harbor a terrible secret. Find it.
Yeah.