goto?? statement?

Hey All,

I have a dumb question.
I have been looking all over the reference manual about this.
I don't see anything.

Does DXL have a command that you can exit/goto another place in the script?
I have inherited code that is just terrible in structure, and since it works, - to a certain point - then I don't want to try and "fix" it. I jsut want to use it and "tweak" it.
The person who did it is using a bunch of IF statements one after the other, which would not be needed once one of the IF statements is valid, then they coudl jump out, but they have t checking each type of thing even if it has satisifed the first one. Poor programming, but again, I did not
do it, I inherited it. I wish I could inherit a bunch of money instead! :)

So, is there such a command that will goto someplace else?
Jerry
SystemAdmin - Thu Jul 28 13:20:57 EDT 2011

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:
}

Re: goto?? statement?
SystemAdmin - Thu Jul 28 13:54:08 EDT 2011

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:
}

Hi Fu Lin,
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?
TheDrizzle - Thu Jul 28 13:55:53 EDT 2011

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 14:00:29 EDT 2011

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
}

I forgot the break statements in my reply but I can't edit so here I am... Either way I am nitpicking. Also OP the break command will break out of the block it is in so most likely it is breaking out of an if statement for you and continuing the loop.

Re: goto?? statement?
SystemAdmin - Thu Jul 28 14:29:12 EDT 2011

TheDrizzle - Thu Jul 28 14:00:29 EDT 2011
I forgot the break statements in my reply but I can't edit so here I am... Either way I am nitpicking. Also OP the break command will break out of the block it is in so most likely it is breaking out of an if statement for you and continuing the loop.

[A] The break statement only works in loops. The only reason I chose a for loop is naked blocks (just the { }) are not allowed.
[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?
llandale - Sun Jul 31 23:17:49 EDT 2011

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.

Anyway...
There is no "goto", there is no function labels.
You can "continue" inside a loop that skips the rest of the loop and starts the next loop counter.
You can "break" inside a loop to goto the bottom of the loop and proceed, doing no more loops.
You can "return" from a function from anywhere in that function.
In your case, I suspect you want an "if-elseif-elseif-else" statement.

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