Welopez’s SELECT CASE Tutorial gives an excellent understanding of how to use SELECT CASE with one variable and even with multiple possible values of that variable. If you were going to write code that assigns a letter grade to a numerical grade, you could certainly use the single variable technique.
INPUT “Numerical Grade: “;score
SELECT CASE score
CASE 100, 99, 98, 97, 96, 95, 94, 93, 92, 91, 90
grade$ = “A”
CASE 89, 88, 87, 96, 95, 84, 83, 82, 81, 80
grade$ = “B”
CASE 79, 78, 77, 76, 75, 74, 73, 72, 71, 70
grade$ = “C”
CASE 69, 68, 67, 66, 65, 64, 63, 62, 91, 60
grade$ = “D”
CASE 59, 58, 57, 56, 55, 54, 53, 52, 51, 50
grade$ = “F”
CASE 59, 48, 47, 46, 45, 44, 43, 42, 41, 40
grade$ = “F”
CASE 39, 38, 37, 36, 35, 34, 33, 32, 31, 30
grade$ = “F”
CASE 29, 28, 27, 26, 25, 24, 23, 22, 21, 20
grade$ = “F”
CASE 19, 18, 17, 16, 15, 14, 13, 12, 11, 10
grade$ = “F”
CASE 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
grade$ = “F”
END SELECT
PRINT “Letter Grade = “;grade$
Aside from the obvious problem with typing so many numbers, there is the added problem not not accommodating all possible numbers. Try entering 97.5 in the above snippet. Not very happy with that blank answer, are you?
Fortunately, SELECT CASE can be used to evaluate one or more conditions rather than one specific variable. When you use this option, do NOT include a variable following SELECT CASE. Rather, refer to the variable in each CASE element.
Staying with this same Numerical Grade to Letter Grade conversion program:
INPUT “Numerical Grade: “;score
SELECT CASE
CASE score >= 90
grade$ = “A”
CASE score >= 80 and score < 90
grade$ = “B”
CASE score >= 70 and score < 80
grade$ = “C”
CASE score >= 60 and score < 70
grade$ = “D”
CASE score < 60
grade$ = “F”
END SELECT
PRINT “Letter Grade = “;grade$
If you understand the SELECT CASE logic, you can streamline the code even further. Once the code has found a true CASE condition, the actions and events specific for that event are executed. The program then ignores all other CASE statements and jumps down to END SELECT. Here’s another look at the same SELECT CASE scenario, this time with even more efficient
INPUT “Numerical Grade: “;score
SELECT CASE
CASE score >= 90
grade$ = “A”
CASE score >= 80
grade$ = “B”
CASE score >= 70
grade$ = “C”
CASE score >= 60
grade$ = “D”
CASE score < 60
grade$ = “F”
END SELECT
PRINT “Letter Grade = “;grade$
Notice that a score of 88 has met the CASE criteria for score >= 80, thus no other CASE selections are considered.
A range of numerical variables are easy to capture using =, <, and >. String variables are not so easily captured. First, there’s the problem with upper and lower case text. “YeS” is not the same as “yEs.” That problem can be solved using UPPER$(text$). See another of Welopez’s excellent tutorials Bullet Proofing User Entries for more tips on standardizing user responses. There still remains, though, the problem of unanticipated user responses. Enter CASE ELSE.
INPUT “What is your favorite animal? “;animal$
animal$ = UPPER$(animal$)
SELECT CASE
CASE animal$ = “CAT”
msg$ = “Oh, me, too! I like Garfield best.”
CASE animal$ = “DOG”
msg$ = “Snoopy is my favorite. I like Scooby Do, too.”
CASE animal$ = “BIRD”
msg$ = “Polly want a cracker? She’d rather have a cookie!”
CASE LEN(animal$) > 7
msg$ = “I don’t think I can even pronounce “;animal$;”!”
CASE ELSE
msg$ = “I don’t know much about the “;animal$;”.”
END SELECT
PRINT msg$
As is the case with multiple variables, multiple conditions can also be used with SELECT CASE. Returning to student grades for a moment, look at these possible responses.
CASE thisGrade > 90 and lastGrade < 80
PRINT “You’ve been studying, and it shows!”
CASE thisGrade < 70 and lastGrade > 85
PRINT “I know you can do better if you study more.”
CASE thisGrade >= 90 and lastGrade >= 90
PRINT “Excellent work in this course!”
The number of CASE conditions are limited only by your imagination (and, possibly, 80MB).
Put an end to long and confusing IF…THEN … ELSE … END IF statements and start taking control with SELECT CASE.
Back to you, Welopez….