Hey All, |
Re: goto?? statement?
void someProcedure() {
// There is no goto but you can use
// the following to implement a
// "pathological exit construct".
// This is useful when there are many
// "get me out of here" conditions. You can use it
// in place of a bunch if nested if blocks for a cleaner
// and less nested look
bool didOnce;
for(didOnce = false; !didOnce; didOnce = true) {
// a bunch of code ...
if (condition1) break; // takes you to END_OF_BLOCK
// a bunch of code ...
if (condition2) break; // takes you to END_OF_BLOCK
// a bunch of code ...
if (condition3) break; // takes you to END_OF_BLOCK
// a bunch of code ...
if (condition4) break; // takes you to END_OF_BLOCK
// a bunch of code ...
if (condition5) break; // takes you to END_OF_BLOCK
// a bunch of code ...
// if you get here didOnce is true so you exit the block anyway
}
// END_OF_BLOCK:
}
|
Re: goto?? statement? SystemAdmin - Thu Jul 28 13:50:33 EDT 2011
void someProcedure() {
// There is no goto but you can use
// the following to implement a
// "pathological exit construct".
// This is useful when there are many
// "get me out of here" conditions. You can use it
// in place of a bunch if nested if blocks for a cleaner
// and less nested look
bool didOnce;
for(didOnce = false; !didOnce; didOnce = true) {
// a bunch of code ...
if (condition1) break; // takes you to END_OF_BLOCK
// a bunch of code ...
if (condition2) break; // takes you to END_OF_BLOCK
// a bunch of code ...
if (condition3) break; // takes you to END_OF_BLOCK
// a bunch of code ...
if (condition4) break; // takes you to END_OF_BLOCK
// a bunch of code ...
if (condition5) break; // takes you to END_OF_BLOCK
// a bunch of code ...
// if you get here didOnce is true so you exit the block anyway
}
// END_OF_BLOCK:
}
YES! I do remember the seeing the BREAK command, but it did not seem to be working. I do not know why I will have to take a look at it again. I may have been trying to break out side of the For loop. But, that looks good what you showed!! Sweet! THanks! Jerry |
Re: goto?? statement? SystemAdmin - Thu Jul 28 13:50:33 EDT 2011
void someProcedure() {
// There is no goto but you can use
// the following to implement a
// "pathological exit construct".
// This is useful when there are many
// "get me out of here" conditions. You can use it
// in place of a bunch if nested if blocks for a cleaner
// and less nested look
bool didOnce;
for(didOnce = false; !didOnce; didOnce = true) {
// a bunch of code ...
if (condition1) break; // takes you to END_OF_BLOCK
// a bunch of code ...
if (condition2) break; // takes you to END_OF_BLOCK
// a bunch of code ...
if (condition3) break; // takes you to END_OF_BLOCK
// a bunch of code ...
if (condition4) break; // takes you to END_OF_BLOCK
// a bunch of code ...
if (condition5) break; // takes you to END_OF_BLOCK
// a bunch of code ...
// if you get here didOnce is true so you exit the block anyway
}
// END_OF_BLOCK:
}
While the for loop method works, a beginner might be confused at first since it seems awkward at first. I think using a while loop is a lot more intuitive but either way works obviously.
While(exit == false)
{
If (cond1) then
exit = true
If (cond2) then
exit = true
exit = true
}
|
Re: goto?? statement? TheDrizzle - Thu Jul 28 13:55:53 EDT 2011 While the for loop method works, a beginner might be confused at first since it seems awkward at first. I think using a while loop is a lot more intuitive but either way works obviously.
While(exit == false)
{
If (cond1) then
exit = true
If (cond2) then
exit = true
exit = true
}
|
Re: goto?? statement? TheDrizzle - Thu Jul 28 14:00:29 EDT 2011 [B] You can only break out of the immediate enclosing loop. Some languages (like Java) offer an optional block label to specify which block { } to break out of. With DXL, it is always the immediate enclosing loop and you'll need additional logic to break out of enclosing blocks { }. |
Re: goto?? statement?
Have not been exposed to recent modern languages, but way back in school they had some language that allowed an inter-function "goto" statement. That is, function "A" could "goto" some labeled statement inside function "B". Even back then I knew that was a disaster as far as writing code that could be understood. If the stuff in B makes sense from the start AND from the middle, then its two functions.
bool FindIt(whatever)
{
for o in entire module do
{ if (isDeleted(o)) continue // not this object, do next one
if (row(o) or (table(o)) continue
if (o is the object I'm looking for)
{ oFound = o; break} // stop looking
}
if (null oFound) return(false) // no such object
elseif (oFound has characteristic A)
{ ...
}
elseif(oFound has characteristic B)
{
}
elseif(oFound has characterisic C)
{
}
else whatever
return(true)
} // end FindIt()
|