Qbasicnews.com

Full Version: CASE scenarios...
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hey everybody... I am kinda confuzzled with the CASE commands 'n stuff. Everybody tells me that using CASE is a lot better than using a bunch of IF-THENS but I really don't know how to use the CASE command. could someone give me an example of CASE? I think that if I got this CASE down then my program could slim down quite a lot.... (I use IF-THEN statements TOO MUCH)

Thanks
Code:
'Hopefully this will work, I'm kinda pulling it out of nowhere
DO
   a$=INPUT$(1)
   a$=RIGHT$(a$,1)
   a$=UCASE$(a$)
   a%=ASC(a$)
   SELECT CASE a%
   case 27   'esc pressed
      PRINT "You Pressed the Esc Key"
      END
   CASE 32   'spacebar pressed
      PRINT "You Pressed the spacebar"
   case 72   'Up pushed
      Print "You pushed up/H"
   END SELECT
   a$=INPUT$(1)
   CLS
LOOP

So, here is how it should work:
choose a variable you want to compare (in the above example, a%) and precede it with a SELECT CASE statement

Then, write CASE anything to have it execute the statements inbetween that CASE and the next CASE when anything is true.

As a note, it does not have to just be a number, you could also say

Code:
CASE IS > 10
'or
CASE IS (>64 and <91) 'checks if a letter key has been pushed

Hope that helps, if you want more, just ask and I will try to explain it.[/code]
Code:
INPUT "Enter a number", N

SELECT CASE N
  CASE 0:
    PRINT "You entered 0!"
  CASE 1,2,3,4,5:
    PRINT "You entered 1,2,3,4, or 5!"
  CASE 6 TO 10:
    PRINT "You entered 6,7,8,9, or 10!"
  CASE IS > 10
    PRINT "You entered a number higher than 10!"
  CASE IS < 0
    PRINT "You entered a negative number!"
  CASE ELSE
    PRINT "You entered a non-integer!"
END SELECT

Get it? Smile

*peace*

Meg.
You use SELECT when you have a single check and different cases. The advantage is the condition is checked only once and not once per case as it would happen with different IFs
OKay, thanks, that clears up pretty much the whole thing for me. Thanks again!