R.T.Russell Home Page

Chapter 10 - Selection using CASE




There are times when you will need to test the same variable for a number of different cases, of which only one will result in any code being executed. An example that springs to mind is a menu system. Let's build it.
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
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%
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
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.

WHEN can have a range of values, modify the CASE statement to add:

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
CASE is just as capable of testing strings:
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
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"
  OTHERWISE: PRINT "Sorry, invalid response"
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.

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

Left CONTENTS

CHAPTER 11 Right


Best viewed with Any Browser Valid HTML 3.2!
© Peter Nairn 2006