Tell me what you want, What you really, really want.

Moderator: Mmiscool

User avatar
By Electroguard
#55187 Would it be possible to add Elseif?

Eg: If option = 1 then gosub [option1]
Elseif option = 2 then gosub [option2]
Elseif option = 3 then gosub [option3]
' etc, etc...
Elseif option = 9 then gosub [option9]
Else print "invalid option"
Endif
User avatar
By livetv
#55209 This would be nice but now that you can branch to label names presented in string variables or expressions (as is discussed in another thread). Your example could be written as follows:

Code: Select allgosub "[x" & word("Option1 Option2 Option3", option) & "]"
' NOTE TO SELF:  Check to see that expressions work as well as variables, otherwise that has to be broken into two lines.

' Then subroutines would look like this:

[xOption1]
'code here
return

[xOption2]
'code here
return

[xOption3]
'code here
return

[x]
print "invalid option"
return


This looks a little cryptic but would run a LOT faster. It gets simpler if you're not trying to capture an error.

What if you have to branch based on something other than nice clean orderly numbers? You could do vector tables of many possibilities pointing to handlers like this:

Code: Select all' Initialize the vector table only once
vtable$=" Greg:[boy] Marcia:[girl] Peter:[boy] Jan:[girl] Bobby:[boy] Cindy:[girl] Tiger:[dog] Mike:[dad] Carol:[mom]"
'   :
' code continues
'   :
person= msgget("visitor")
place=instr(vtable$," " & person & ":")
if place > 0 then vector$= word( mid(vtable$, place), 2,":") else vector$="[notfound]"
gosub  vector$
' -- code continues --

' here we define subroutines [boy], [girl], [dog], [notfound]


This beats a whole lot of ifelse statements!
User avatar
By Electroguard
#55218 It was actually from exploring doing menu's using branching by variables, and trying to trap and debug the problems of udp branching on variables and the need to test for problems such as no matching branches for all possible variables, which showed up how convenient an Elseif instruction would be... both for menus and error-trapping.

So while I had already recognised your point about how useful branching by variables is going to be, I don't think that ought to detract from how useful and simple a ew Elseif instruction might be, and might benefit Esp_Basic if it was something our magician could achieve.

I like your thinking about vector variables by the way, and I think this latest bit of branching by variables magic is going to offer all sorts of possibilities for manipulating and combining variables into clever branch options.

Which brings up another suggestion Mmiscool, if you're listening...

At the moment, if the script instructs the program to branch to a non-existent branch it causes the program to fail with a branch error.
This is going to be much more common now that branching by variables is possible.

So would it be possible to trap branch errors before they actually become errors branching to non-existent branches?

When the script specifies a branch, could the program confirm that branch exists BEFORE jumping to it, and if the destination branch doesn't exist, could it take some appropriate avoiding action, such as branch to a default 'brancherror' subroutine if it exists, else simply 'print [branch] does not exist'.

This could then be used as a 'catchall' for invalid branching on variables, but it would also be a very useful debugging aid for mis-spelt branch errors etc.