Page 2 of 2

Posted: Fri Jan 27, 2006 7:01 am
by Z!re
Nekrophidius wrote:
Z!re wrote:

Code: Select all

DO
k$ = INKEY$
LOOP UNTIL k$ = "w"
SELECT CASE k$
[...]
You bonehead...that specific code is never gonna work. :D k$ is ALWAYS going to be w after the loop because the loop won't end until it is so your SELECT CASE will only ever work with CASE "w". :P Silly Swede... ;)
Yes, just like all previous code snippets..

Posted: Fri Jan 27, 2006 12:52 pm
by Zim
(PaulUnknown's "nevermine" notwithstanding...)

Just remember, if you're going to use SELECT CASE after a keyboard input, you'd better have a CASE ELSE at the end of your case list, or you'll have to have a CASE for every key on the keyboard! Otherwise, after the first keystroke you're not checking for, the program bombs.

Posted: Fri Jan 27, 2006 10:45 pm
by moneo
Zim wrote:(PaulUnknown's "nevermine" notwithstanding...)

Just remember, if you're going to use SELECT CASE after a keyboard input, you'd better have a CASE ELSE at the end of your case list, or you'll have to have a CASE for every key on the keyboard! Otherwise, after the first keystroke you're not checking for, the program bombs.
Very sound advice, Zim. Hope it doesn't fall on deaf ears. What'd you say? :shock:

Posted: Sun Jan 29, 2006 3:24 pm
by paulunknown
Just remember, if you're going to use SELECT CASE after a keyboard input, you'd better have a CASE ELSE at the end of your case list, or you'll have to have a CASE for every key on the keyboard! Otherwise, after the first keystroke you're not checking for, the program bombs.
Sorry but I don't know what you are saying.

Posted: Mon Jan 30, 2006 9:27 pm
by {Nathan}
If you use a Select Case, and you don't use a CASE ELSE,
then you will end up doing this.

Code: Select all

SELECT CASE Cheese$
 CASE IS "A" 
  ...
 CASE IS "B"
  ...
 CASE IS "C"
  PRINT "TRY AGAIN"
 CASE IS "D"
  PRINT "TRY AGAIN"
 CASE IS "E"
  PRINT "TRY AGAIN"
 CASE IS "F"
  PRINT "TRY AGAIN"
...
 CASE IS "Z"
  PRINT "TRY AGAIN"
You get what I mean, right? Typing out all of the letters? Yeah, thats right... but instead you can do this:

Code: Select all

SELECT CASE Cheese$
 CASE IS "A"
  PRINT "YOU WIN!"
 CASE IS "B"
  PRINT "YOU WIN!"
 CASE ELSE
  PRINT "YOU FAIL!"
Yeah, you get it now? But if you command your CASEs to use subs/functions/GOTOs, then it will exit without doing anything which can REALLY screw your game/program/OS up...

*caugh* No, I didn't say just like in windows... *caugh*

I hope you got that...

Posted: Wed Feb 01, 2006 11:25 am
by Zim
GoTo's can be dangerous, but subs and functions should be ok... no?

Posted: Wed Feb 01, 2006 11:52 pm
by paulunknown
Well that's what I wanted to do.
If I limited the cases possible before SELECT CASE then I don't need CASE ELSE, right?

Posted: Thu Feb 02, 2006 12:35 pm
by Zim
Right!

But you have to be absolutely sure that (at least) ONE of the cases is true, else the program will bomb. I usually do something like this:

Code: Select all


Input "Option (1) or (2) ", k

Select Case k
   case 1 : print "You said ONE"
   case 2 : print "You said TWO"
   case else 'do nothing
End Select

Adding that one little line with a comment " 'do nothing " prevents the program from an abnormal abort. Very little code; good insurance; lots of bang for the buck.

Posted: Thu Feb 02, 2006 9:25 pm
by moneo
Zim wrote:Right!

But you have to be absolutely sure that (at least) ONE of the cases is true, else the program will bomb. I usually do something like this:

Code: Select all


Input "Option (1) or (2) ", k

Select Case k
   case 1 : print "You said ONE"
   case 2 : print "You said TWO"
   case else 'do nothing
End Select

Adding that one little line with a comment " 'do nothing " prevents the program from an abnormal abort. Very little code; good insurance; lots of bang for the buck.
Good point, Zim. But if the user hits anything other than 1 or 2, he would get absolutely no output response and wonder ???.

I would enhance your code as follows:

Code: Select all

Input "Option (1) or (2) ", k
Select Case k
   case 1 : print "You said ONE"
   case 2 : print "You said TWO"
   case else : print "Error: Invalid Option"
End Select 
*****

Posted: Fri Feb 03, 2006 11:08 am
by Zim
Even better!