Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
CASE scenarios...
#1
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
ovaProgramming.

One night I had a dream where I was breaking balls. The next morning, BALLSBREAKER was born.

Quote: Excellent. Now you can have things without paying for them.

BALLSBREAKER 2
~-_-Status Report-_-~
Engine: 94%
Graphics: 95%
Sound: 100%
A Severe Error has crippled BB2 for the time being... I have to figure it out, but until then you won't see much of it Sad.
-----------------------------
Reply
#2
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]
Reply
#3
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.
Reply
#4
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
Antoni
Reply
#5
OKay, thanks, that clears up pretty much the whole thing for me. Thanks again!
ovaProgramming.

One night I had a dream where I was breaking balls. The next morning, BALLSBREAKER was born.

Quote: Excellent. Now you can have things without paying for them.

BALLSBREAKER 2
~-_-Status Report-_-~
Engine: 94%
Graphics: 95%
Sound: 100%
A Severe Error has crippled BB2 for the time being... I have to figure it out, but until then you won't see much of it Sad.
-----------------------------
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)