SELECT CASE I

        In qbasic there are 2 major different types of selection statements. The IF statement, that we've already discussed, and the SELECT CASE statement. In my opinion the SELECT CASE statement is one of the most versatile constructs in QBasic. This construct is found in many other languages, however I've found that this is the most versatile implementation that I've run across. Due to the fact that there is so much that you can do with CASE statements, I've broken this portion down into two sections, the first is intended to just give an idea about how one works, and the second part is the specifics of what we can do.

        The SELECT CASE statement is best used when you have several different cases that you want test for. For example say that you write a program that does something based on a number, say that each number between 1 and 10 is to do something different. This can get pretty tedious and even somewhat unreliable if you use an IF statement.To fix this problem there are CASE stetements. A CASE statement can be used like this:

        INPUT "Please Enter a Value", val%
        SELECT CASE val%
            CASE 1: PRINT "YOUR VALUE IS ONE"
            CASE 2: PRINT "HELLO THERE"
            CASE 3: PRINT "HMMMMNN"
        END SELECT

Output:
        Please Enter a Value 2
        HELLO THERE

        As you can see the main idea is similar to that of an IF statement. It is really very simple. If you wish to use strings for the variable to be SELECTed from, you can do that like this:

        INPUT "Please Enter a letter", letter$
        SELECT CASE letter$
            CASE "a": PRINT "Your letter is a"
            CASE "b": PRINT "Hello there"
            CASE "c": PRINT " a b c d e f g, now I know my musical keys!"
        END SELECT

Output:
        Please Enter a letter b
        Hello there

        The good thing about CASE statements is that if you change the value of the variable being tested, another piece of code is not executed somewhere down the line that shouldn't. For example if you were to do some operation where the value being tested is changed to say a value that was tested after the line where it was changed. You won't get an error where the wrong piece of code is executed. Example:

As an IF statement:
        a% = 10
          IF a% = 10 THEN a% = a% + 1
          IF a% = 11 THEN a% = a% + 5
        PRINT "The value"; a% ;"is wrong."

Output:
        The value 16 is wrong.

As a CASE statement
        a% = 10
        SELECT CASE a%
          CASE 10: a% = a% + 1
          CASE 11: a% = a% + 5
        END SELECT

        PRINT "The value"; a%; "is correct."

Output:
        The value 11 is correct.

        As you can see the IF statement produced an incorrect result. However, this can be corrected in this case with an ELSE clause, however this example does illustrate the usefullness of the CASE statement. One thing that you may have noticed this far is that if there is no correct input, what happens? Well, nothing. This can be desireable in some cases, and undesireable in others. To fix this problem, if it becomes necessary, is to use the ELSE CASE statement. If no other cases are met then this one is performed. It is good practice to use this to handle unforseen input. The Use would be this:

        a% = 99
        SELECT CASE a%
          CASE 10: a% = a% + 1
          CASE 11: a% = a% + 5
          ELSE CASE: PRINT "No value, quitting" : END
        END SELECT
        PRINT "The value"; a%; "is correct."

Output:
        No value, quitting

        As you can see in this example, there was case for the value of 99. So, the ELSE CASE was performed instead.
        The block of code to be performed for a specific case does not all have to be on one line. You can put it on as many lines as needed. This is done, using the example above, in this way:

        a% = 99
        SELECT CASE a%
                  CASE 10
                            a% = a% + 1
                  CASE 11
                            a% = a% + 5
                  ELSE CASE
                            PRINT "No value, quitting"
                            END
        END SELECT
        PRINT "The value"; a%; "is correct."

Output:
        No value, quitting

        There are two things that you should notice from this example. The first, although not part of the current discussion, is the indentation that I have done, I have done this in previous examples, however, not to this extent. The reason I indented so much is to clarify what is happening. If you have a 'nested' or 'deeply nested' set of statements. That is to say, you have pieces within pieces of code that are all performed as a group, or block, this is called nesting. Indention is used to denote the 'depth' of the nesting. In this case 2 levels. This is not that important, the only reason to know it is that it is important to do so that when you go back to read your code after it has been written you know what is going on much more easily.
        Okay the second thing I wanted you to notice is that the (:) is missing. When you put the code on a different line, it becomes unecessary to place the (:) after the case value, although you can if you want to.

        Okay now that you've got the general idea let's move on to the specifics!

    Back         Home         Next