IF...THEN...ELSE is fine if you only have two things you want to check. What if you have 5 or 6 friends that might use your computer and you want the computer to say something different to each of them? Try this:
CLS
INPUT "Enter your name: ", Name$
SELECT CASE Name$
CASE "Ted"
PRINT "Greetings, oh powerful master"
CASE "Mike"
PRINT "Go away!"
CASE ELSE
PRINT "Hello, "; Name$; ". How are you?"
END SELECT
Whew, that was a big one. Fortunately we learned how to save in Chapter 8. Save it if you want before running it. Feel free to change "Ted" and "Mike" to "Laura" and "Robin" or whoever.
SELECT CASE
SELECT CASE first checks Name$ for the value "Ted". If it finds it, it does the PRINT after the CASE "Ted". When the PRINT is done, it skips over the rest of the CASEs. It keeps checking against each CASE until it gets to CASE ELSE. If it hasn't found anything, it will do whatever is after the CASE ELSE.
Just In CASE
SELECT CASE can also be used with numbers as well as strings. Here's a quick example:
CLS
INPUT "Enter a number: ", Number
SELECT CASE Number
CASE 1234
PRINT "Thank you for entering the secret number 1234"
CASE 22
PRINT "Well, 22 is an interesting number"
CASE ELSE
PRINT "You must not know the secret number"
END SELECT
Learned
- SELECT CASE

No comments:
Post a Comment