switch... case 1:

SYS 64738
User avatar
Shaun_B
Member
Member
Posts: 179
Joined: Tue May 06, 2014 12:12 pm
Location: UK
Contact:

switch... case 1:

Post by Shaun_B »

Hi all,

Someone on IRC asked if there was a way of doing something similar to the switch keyword in PHP/C++ etc... I couldn't think of an easy way but came up with this:

Code: Select all

10 let case=6
20 if case<7 then on case gosub 100, 120, 140, 160, 180, 200:if case>0 then goto 40
30 print "default"
40 end
100 print "condition 1"
110 return
120 print "condition 2"
130 return
140 print "condition 3"
150 return
160 print "condition 4"
170 return
180 print "condition 5"
190 return
200 print "condition 6"
210 return
Now the question is, how do you determine your case? For instance, you could:

Code: Select all

10 get case$: if case$<"0" or case$>"6" then goto 10
15 case = val(case$)
20 on case gosub 100, 120, 140, 160, 180, 200:if case>0 then goto 40
That way, you've already done your check in line 10 whilst waiting for a legal key entry.

Regards,

Shaun.


BASIC Programming - making the mistakes so that you don't have to.
Circles and Squares.
Nothing I post here will stand up in a court of law.
MarcWalters
Member
Member
Posts: 55
Joined: Wed Jun 11, 2014 2:33 pm
Location: Lake Macquarie, NSW, Australia
Contact:

Re: switch... case 1:

Post by MarcWalters »

My personal preference is for separate input validation, and have the ON statement's logic handle the default condition.

Code: Select all

1 def fnv(x) = (x > -1) and (x < 7) : rem return TRUE if in range 0..6
...
10 get case$: if not fnv( val( case$ ) ) then 10  
30 on ( 1 + val( case$ ) ) gosub 220, 100, 120, 140, 160, 180, 200
40 end
100 print "condition 1"
110 return
...
220 print "default"
230 return
Also, the boolean result from a comparison operation can be used to avoid conditional tests. For example, the following shows the use of subroutines for default (line 999) and cases "Y" and "N":

Code: Select all

10 print "Press Y or N" : input case$
20 on 1 + -(case$="Y") + -2*(case$="N") gosub 999,200,300
...
User avatar
Shaun_B
Member
Member
Posts: 179
Joined: Tue May 06, 2014 12:12 pm
Location: UK
Contact:

Re: switch... case 1:

Post by Shaun_B »

I knew that there'd be a better way to do it, thanks Marc.

Regards,

Shaun.
BASIC Programming - making the mistakes so that you don't have to.
Circles and Squares.
Nothing I post here will stand up in a court of law.
User avatar
buzbard
Member
Member
Posts: 24
Joined: Sun Apr 13, 2014 9:06 pm
Location: Washington State
Contact:

Re: switch... case 1:

Post by buzbard »

There's really no need to use an "IF" statement here since the "ON" statement is a variant of the "IF" statement.
The "ON" statement doesn't have a condition for zero, the first condition is always one.
Your list of conditions after the "GOSUB" will take care of values that are too large.
So, your code can be simplified to this:

Code: Select all

20 get case$: on val(case$) gosub 100, 120, 140, 160, 180, 200: goto 20
The "GET" statement won't allow negative values, the "VAL" statement will return zero for everything except 1 to 9, and your list of conditions will take care of the rest.
Ray...
It's Ok if you don't agree with me, I can't force you to be right.
User avatar
Shaun_B
Member
Member
Posts: 179
Joined: Tue May 06, 2014 12:12 pm
Location: UK
Contact:

Re: switch... case 1:

Post by Shaun_B »

buzbard wrote:

Code: Select all

20 get case$: on val(case$) gosub 100, 120, 140, 160, 180, 200: goto 20
Good tip Buzbard, I suppose it depends on what you're doing but I'd still be tempted to put a sanity check in there, like:

Code: Select all

20 get case$: on val(case$) gosub 100, 120, 140, 160, 180, 200: if case$="" then goto 20
or something. Although that may not be required in all cases.

Regards,

Shaun.
BASIC Programming - making the mistakes so that you don't have to.
Circles and Squares.
Nothing I post here will stand up in a court of law.
User avatar
buzbard
Member
Member
Posts: 24
Joined: Sun Apr 13, 2014 9:06 pm
Location: Washington State
Contact:

Re: switch... case 1:

Post by buzbard »

Shaun_B wrote: Good tip Buzbard, I suppose it depends on what you're doing but I'd still be tempted to put a sanity check in there, like:

Code: Select all

20 get case$: on val(case$) gosub 100, 120, 140, 160, 180, 200: if case$="" then goto 20
or something. Although that may not be required in all cases.
You could do that but it's not necessary because the VAL of "" is still zero.

Code: Select all

10 get case$: print val(case$): goto 10
I learned to program on an unexpanded VIC20, with only 3.5k for BASIC, so I have a habit of compressing code as much as possible.
I still do it today with other programming languages, sometimes to the point of sacrificing speed. :?
Ray...
It's Ok if you don't agree with me, I can't force you to be right.
User avatar
Shaun_B
Member
Member
Posts: 179
Joined: Tue May 06, 2014 12:12 pm
Location: UK
Contact:

Re: switch... case 1:

Post by Shaun_B »

buzbard wrote:I learned to program on an unexpanded VIC20, with only 3.5k for BASIC, so I have a habit of compressing code as much as possible.
I still do it today with other programming languages, sometimes to the point of sacrificing speed. :?
Compressed and minimising programs is a good thing - especially if you use JS :-) but I would usually minimise after I'm confident my listing is error free at least professionally. I'm a bit more lazy with Commodore programming.

What I should have added is that when the 'case' is returned, it will automatically goto 20 even if you don't want it to with your improvement. That was the reason that I added the check (assuming that the string case$ - or ca$ as the interpreter will see it - isn't emptied elsewhere). If you want it to automatically loop back on return from the 'case' then there's no need for the check :-)

Regards,

Shaun.
BASIC Programming - making the mistakes so that you don't have to.
Circles and Squares.
Nothing I post here will stand up in a court of law.
MarcWalters
Member
Member
Posts: 55
Joined: Wed Jun 11, 2014 2:33 pm
Location: Lake Macquarie, NSW, Australia
Contact:

Re: switch... case 1:

Post by MarcWalters »

Here's one way to allow a graceful exit from an input-switch on the condition that an input has been successfully handled.
The flag, Frepeat is set prior to entering the input-switch. A switch subroutine clears the flag on exit.

Code: Select all

19  Frepeat = 1 : rem 1= true, 0 = false
20 get case$: on val(case$) gosub 100, 120, 140, 160, 180, 200: on Frepeat goto 20
...
100 print "condition 1"
110 Frepeat = 0 : return
...
User avatar
buzbard
Member
Member
Posts: 24
Joined: Sun Apr 13, 2014 9:06 pm
Location: Washington State
Contact:

Re: switch... case 1:

Post by buzbard »

Except "FRE" is a keyword and line 19 will cause a Syntax Error.
Ray...
It's Ok if you don't agree with me, I can't force you to be right.
User avatar
Shaun_B
Member
Member
Posts: 179
Joined: Tue May 06, 2014 12:12 pm
Location: UK
Contact:

Re: switch... case 1:

Post by Shaun_B »

buzbard wrote:Except "FRE" is a keyword and line 19 will cause a Syntax Error.
Good spot - change it to srepeat or switch, so sr or sw respectively.

Regards,

Shaun.
BASIC Programming - making the mistakes so that you don't have to.
Circles and Squares.
Nothing I post here will stand up in a court of law.
Post Reply Previous topicNext topic

Who is online

Users browsing this forum: No registered users and 7 guests