Well, in a structured pseudo-code it would be:
Code: Select all
do
k$=inkey$
loop while k$<>"1" and k$<>"2"
case k$
"1": blah blah blah for case 1
"2": blah blah blah for case 2
end case
So my first attempt might be:
Code: Select all
100 LET K$=INKEY$
110 IF K$<>"1" AND K$<>"2" THEN GOTO 100
120 IF K$="1" THEN GOTO 1000
130 IF K$="2" THEN GOTO 2000
140 STOP
1000 blah blah blah for case 1
1010 blah blah blah for case 1
1020 GOTO 140
2000 blah blah blah for case 2
2010 blah blah blah for case 2
2020 GOTO 140
You could do a calculated GOSUB to avoid having extra GOTOs back to the end of the case list. Then it would be:
Code: Select all
100 LET K$=INKEY$
110 IF K$<>"1" AND K$<>"2" THEN GOTO 100
120 GOSUB 1000*VAL K$
130 STOP
1000 blah blah blah for case 1
1010 blah blah blah for case 1
1020 RETURN
2000 blah blah blah for case 2
2010 blah blah blah for case 2
2020 RETURN
That's a bit nicer. If K$ doesn't happen to be always 1 or 2, but could be other keys such as A and B, you could do:
Code: Select all
100 LET K$=INKEY$
110 IF K$<>"A" AND K$<>"B" THEN GOTO 100
120 IF K$="A" THEN GOTO 150
130 IF K$="B" THEN GOTO 170
140 GOTO 180
150 GOSUB 1000
160 GOTO 180
170 GOSUB 2000
180 STOP
This has the advantage that if you want to do a fall-through from one case to another (like in C), so that the case for A also falls through into the case for B, you would just remove line 160. Even though there are now a few GOTOs, it's still very nicely structured and maintainable, with no nasty surprises or "spaghetti coding". You can remove line 120 and line 140 in fact, as you know that it can only be A and B in this case, but I've just coded the "case" generically from the pseudo-code.
Another way of handling more generic keys would be to set up a string of options, e.g. O$="ABXH12", and then have a routine that found the index position of the key in that string. So if you pressed "A", it would return 1, and if you pressed "1" then it would return 5. You could then either use the index to calculate the line number to use, or use it to look up the line number in an array. That approach might be quite good if there were lots of different menus in your application, and you wanted them to use the same code to read the keypress and jump to the correct subroutine. You could use the same code, but just redefine O$ and the line number array depending on which menu you were in. That would be overkill for the example you gave, though.