If the user chooses 1, the code associated with choice 1 is run. When finished BASIC goes off and checks if Choice% is 2 or 3 even though it clearly cannot be. This is not very efficient and, if each option evolves into several lines of code, it becomes easy to lose track of the IFs. Enter IF's bigger brother: CASE. We'll rewrite the program using CASE:
REM Simple menu system CLS PRINT "Press 1 for option 1" PRINT "Press 2 for option 2" PRINT "Press 3 for option 3" INPUT "Enter choice: " Choice% IF Choice%=1 THEN PRINT "You chose option 1" IF Choice%=2 THEN PRINT "You chose option 2" IF Choice%=3 THEN PRINT "You chose option 3" END
I'm sure you can see what's happening here. The CASE line tells BASIC it's using Choice% as a comparison variable. Each WHEN line gives a value to compare with. Upon finding a match, the associated code is run up to the next WHEN. BASIC then jumps to the ENDCASE statement and continues the rest of the program, ignoring all the other WHENs. As with ENDIF, ENDCASE is always one word.
REM Simple menu system CLS PRINT "Press 1 for option 1" PRINT "Press 2 for option 2" PRINT "Press 3 for option 3" INPUT "Enter choice: " Choice% CASE Choice% OF WHEN 1: PRINT "You chose option 1" WHEN 2: PRINT "You chose option 2" WHEN 3: PRINT "You chose option 3" ENDCASE END
WHEN can have a range of values, modify the CASE statement to add:
CASE is just as capable of testing strings:
CASE Choice% OF WHEN 1: PRINT "You chose option 1" WHEN 2: PRINT "You chose option 2" WHEN 3: PRINT "You chose option 3" WHEN 4,5,6: PRINT "Option not yet implemented." ENDCASE
If none of the WHENs are executed, we can catch the fact and do something about it. The keyword OTHERWISE must be the last case before ENDCASE and has no conditions.
REM Geography quiz PRINT "What is the capital of France:" PRINT "a) Paris" PRINT "b) London" PRINT "c) Madrid" INPUT "Enter a,b or c: " Reply$ CASE Reply$ OF WHEN "A", "a": PRINT "Correct" WHEN "B", "b": PRINT "Sorry, that's England" WHEN "C", "c": PRINT "Sorry, that's Spain" ENDCASE END
It is, as stated, optional. Each WHEN could be, and often will be more than one line of code. If the tests in the WHENs overlap or are the same, the first one is executed and the second one ignored.
REM Geography quiz PRINT "What is the capital of France:" PRINT "a) Paris" PRINT "b) London" PRINT "c) Madrid" INPUT "Enter a,b or c: " Reply$ CASE Reply$ OF WHEN "A", "a": PRINT "Correct" WHEN "B", "b": PRINT "Sorry, that's England" WHEN "C", "c": PRINT "Sorry, that's Spain" OTHERWISE: PRINT "Sorry, invalid response" ENDCASE END
That's about it for CASE, very useful, especially when dealing with subroutines later on. One final word of warning, however, don't be tempted to put a comment (REM) after the OF or the whole thing won't work and you'll be scratching your head as to why.
Exercise
Recreate an executive decision maker. Generate a random number 1 to 6 and print a message depending on the result:
1 - Hire a yes man
2 - Fire someone
3 - Delegate
4 - Cancel all overtime
5 - Give yourself a rise
6 - Raid the pension fund
CONTENTS |
CHAPTER 11 |