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
        Please Enter a Value 2
        HELLO THERE
        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
        Please Enter a letter b
        Hello there
As an IF statement:
        PRINT "The value"; a%; "is correct."
        a% = 10
          IF a% = 10 THEN a% = a% + 1
          IF a% = 11 THEN a% = a% + 5
        PRINT "The value"; a% ;"is wrong."
        The value 16 is wrong.
        a% = 10
        SELECT CASE a%
          CASE 10: a% = a% + 1
          CASE 11: a% = a% + 5
        END SELECT
        The value 11 is correct.
        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."
        No value, quitting
        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."
        No value, quitting
        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     |