Xojo EXIT command

Question about the various forms of EXIT in Xojo

  1. EXIT SUB/FUNCTION is the same as RETURN but unable to return a “value”
  2. EXIT DO/WHILE only exit their own loop and cannot exit an outter loop?
  3. EXIT FOR (without a variable) only exit their own loop and cannot exit an outter loop?
  4. EXIT FOR x … exits the x loop regardless of nested loops
For i=1 to 10
   for j=1 to 10
    Exit For // exits only the J loop
   Next j
Next i

For i=1 to 10
   for j=1 to 10
      Exit For i // exits the I (outter)
   Next j
Next i

For i=1 to 10
   While j<10
      j=j+1
      If j=5 Then Exit While
   Wend
Next i

Making only #4 the most “complex” … the rest just require replacement with either “RETURN” or “BREAK” in Swift

Wow, I didn’t know you could exit outer loops, and it never dawned on me to try that. Neat!

You can also do the “Exit For i” inside the while loop to exit the outer “for i” loop, but with nested whiles I couldn’t exit the outermost one.

1 Like

If your working on Xojo to Swift code conversion, then don’t forget Swift for loops can be labeled, and exited from inner nested loops, using the break keyword, like this.

outerLoop: for i in 1...20 {
    if i % 2 != 0 { // If odd number
        continue // Next iteration of outerLoop
    }
    innerLoop: for x in 1...5 {
        if i > 10 { // i is higher than 10
            break outerLoop // Exit outerLoop
        }
        print("\(i) + \(x) = \(i + x)")
    }
}

Yes, I was aware… which is actually why I stated the question the way I did… to be sure the the only time I should need that construct is where a “EXIT FOR” included a variable relating to an outer loop

Yes you are correct.
I don’t remember if RB / Xojo has an equivalent of the ‘continue’ keyword.

Yes … it’s Continue :wink:

Though in API 2 it might eventual be:
TruncateThisLoopAndSkipToTheEndOfThisItterationImmediately

-karen

3 Likes

Thanks Karen. :+1:

It’s been over a decade since I last used what was then called Real Basic.
Amazing how quick we get old, and how quick we loose our memories. :man_shrugging:

1 Like